NamedPipeLogger.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Diagnostics;
  2. using InABox.Formatters;
  3. using H.Pipes;
  4. namespace InABox.Logging
  5. {
  6. public class NamedPipeLogger : LoggerBase
  7. {
  8. private PipeServer<CoreFormattableString> _pipe;
  9. private string _name = "";
  10. public NamedPipeLogger(string name = "")
  11. {
  12. _name = string.IsNullOrWhiteSpace(name) ? Process.GetCurrentProcess().ProcessName : name;
  13. _pipe = new PipeServer<CoreFormattableString>(_name, new CoreFormatter<CoreFormattableString>());
  14. _pipe.ClientConnected += _pipe_ClientConnected;
  15. _pipe.StartAsync();
  16. }
  17. private void _pipe_ClientConnected(object? sender, H.Pipes.Args.ConnectionEventArgs<CoreFormattableString> e)
  18. {
  19. _pipe.WriteAsync(new CoreFormattableString($"Connected to {_name}"));
  20. }
  21. public override void Stop()
  22. {
  23. _pipe.StopAsync().Wait();
  24. }
  25. protected override void DoSend(string message)
  26. {
  27. if (_pipe.ConnectedClients.Any())
  28. _pipe.WriteAsync(new CoreFormattableString(message));
  29. }
  30. }
  31. }