From 29c3fe22b1a5767f05b8957eeb1db0ba063b8f14 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Mon, 20 Nov 2023 14:55:30 -0700 Subject: [PATCH] fix: adds text formatter --- src/Pepperdash Core/Logging/Debug.cs | 3 +- .../Logging/DebugWebsocketSink.cs | 30 +++++++++---------- src/Pepperdash Core/PepperDash_Core.csproj | 5 ++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Pepperdash Core/Logging/Debug.cs b/src/Pepperdash Core/Logging/Debug.cs index 1089fb0..4746755 100644 --- a/src/Pepperdash Core/Logging/Debug.cs +++ b/src/Pepperdash Core/Logging/Debug.cs @@ -11,6 +11,7 @@ using Serilog; using Serilog.Core; using Serilog.Events; +using Serilog.Formatting.Compact; namespace PepperDash.Core { @@ -106,7 +107,7 @@ static Debug() { _consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Information); _websocketLoggingLevelSwitch = new LoggingLevelSwitch(); - _websocketSink = new DebugWebsocketSink(); + _websocketSink = new DebugWebsocketSink(new CompactJsonFormatter()); // Instantiate the root logger _logger = new LoggerConfiguration() diff --git a/src/Pepperdash Core/Logging/DebugWebsocketSink.cs b/src/Pepperdash Core/Logging/DebugWebsocketSink.cs index c11ee55..3cb0d49 100644 --- a/src/Pepperdash Core/Logging/DebugWebsocketSink.cs +++ b/src/Pepperdash Core/Logging/DebugWebsocketSink.cs @@ -15,6 +15,9 @@ using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certificate2; using System.IO; using Org.BouncyCastle.Asn1.X509; +using Serilog.Formatting; +using Newtonsoft.Json.Linq; +using Serilog.Formatting.Json; namespace PepperDash.Core { @@ -47,11 +50,13 @@ public string Url public bool IsRunning { get => _httpsServer?.IsListening ?? false; } - private readonly IFormatProvider _formatProvider; + private readonly ITextFormatter _textFormatter; - public DebugWebsocketSink() + public DebugWebsocketSink(ITextFormatter formatProvider) { + _textFormatter = formatProvider ?? new JsonFormatter(); + if (!File.Exists($"\\user\\{_certificateName}.pfx")) CreateCert(null); @@ -104,16 +109,11 @@ public void Emit(LogEvent logEvent) { if (_httpsServer == null || !_httpsServer.IsListening) return; - var message = logEvent.RenderMessage(_formatProvider); - _httpsServer.WebSocketServices.Broadcast(message); + var sw = new StringWriter(); + _textFormatter.Format(logEvent, sw); + + _httpsServer.WebSocketServices.Broadcast(sw.ToString()); - foreach(var service in _httpsServer.WebSocketServices.Hosts) - { - foreach (var session in service.Sessions.Sessions) - { - - } - } } public void StartServerAndSetPort(int port) @@ -121,10 +121,10 @@ public void StartServerAndSetPort(int port) Debug.Console(0, "Starting Websocket Server on port: {0}", port); - Start(port, $"\\user\\{_certificateName}.pfx", _certificatePassword, @"/"); + Start(port, $"\\user\\{_certificateName}.pfx", _certificatePassword); } - private void Start(int port, string certPath = "", string certPassword = "", string rootPath = @"/html") + private void Start(int port, string certPath = "", string certPassword = "") { try { @@ -210,9 +210,9 @@ public static class DebugWebsocketSinkExtensions { public static LoggerConfiguration DebugWebsocketSink( this LoggerSinkConfiguration loggerConfiguration, - IFormatProvider formatProvider = null) + ITextFormatter formatProvider = null) { - return loggerConfiguration.Sink(new DebugWebsocketSink()); + return loggerConfiguration.Sink(new DebugWebsocketSink(formatProvider)); } } diff --git a/src/Pepperdash Core/PepperDash_Core.csproj b/src/Pepperdash Core/PepperDash_Core.csproj index db72046..26d13f2 100644 --- a/src/Pepperdash Core/PepperDash_Core.csproj +++ b/src/Pepperdash Core/PepperDash_Core.csproj @@ -36,8 +36,9 @@ Full - - + + +