1234567891011121314151617181920212223242526272829303132333435 |
- using System.Diagnostics;
- using InABox.Formatters;
- using H.Pipes;
- namespace InABox.Logging
- {
- public class NamedPipeLogger : LoggerBase
- {
- private PipeServer<CoreFormattableString> _pipe;
- private string _name = "";
- public NamedPipeLogger(string name = "")
- {
- _name = string.IsNullOrWhiteSpace(name) ? Process.GetCurrentProcess().ProcessName : name;
- _pipe = new PipeServer<CoreFormattableString>(_name, new CoreFormatter<CoreFormattableString>());
- _pipe.ClientConnected += _pipe_ClientConnected;
- _pipe.StartAsync();
- }
- private void _pipe_ClientConnected(object? sender, H.Pipes.Args.ConnectionEventArgs<CoreFormattableString> e)
- {
- _pipe.WriteAsync(new CoreFormattableString($"Connected to {_name}"));
- }
- public override void Stop()
- {
- _pipe.StopAsync().Wait();
- }
- protected override void DoSend(string message)
- {
- if (_pipe.ConnectedClients.Any())
- _pipe.WriteAsync(new CoreFormattableString(message));
- }
- }
- }
|