Skip to content

Commit

Permalink
fix: adds text formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ndorin committed Nov 20, 2023
1 parent 6c51ce5 commit 29c3fe2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/Pepperdash Core/Logging/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Compact;

namespace PepperDash.Core
{
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 15 additions & 15 deletions src/Pepperdash Core/Logging/DebugWebsocketSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -104,27 +109,22 @@ 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)
{
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
{
Expand Down Expand Up @@ -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));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Pepperdash Core/PepperDash_Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.2">
<Aliases>Full</Aliases>
</PackageReference>
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" />
</ItemGroup>
Expand Down

0 comments on commit 29c3fe2

Please sign in to comment.