using System.Diagnostics; using InABox.Formatters; using H.Pipes; namespace InABox.Logging { public class NamedPipeLogger : LoggerBase { private PipeServer _pipe; private string _name = ""; public NamedPipeLogger(string name = "") { _name = string.IsNullOrWhiteSpace(name) ? Process.GetCurrentProcess().ProcessName : name; _pipe = new PipeServer(_name, new CoreFormatter()); _pipe.ClientConnected += _pipe_ClientConnected; _pipe.StartAsync(); } private void _pipe_ClientConnected(object? sender, H.Pipes.Args.ConnectionEventArgs 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)); } } }