Skip to content

Commit

Permalink
fix: update logging format to include key
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-welker committed Mar 8, 2024
1 parent bf7b1f4 commit ada8b7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,4 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
*.projectinfo
19 changes: 10 additions & 9 deletions src/Pepperdash Core/Logging/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public static class Debug
private static Dictionary<uint, LogEventLevel> _logLevels = new Dictionary<uint, LogEventLevel>()
{
{0, LogEventLevel.Information },
{1, LogEventLevel.Warning },
{2, LogEventLevel.Error },
{3, LogEventLevel.Fatal },
{4, LogEventLevel.Debug },
{5, LogEventLevel.Verbose },
{3, LogEventLevel.Warning },
{4, LogEventLevel.Error },
{5, LogEventLevel.Fatal },
{1, LogEventLevel.Debug },
{2, LogEventLevel.Verbose },
};

private static Logger _logger;
Expand Down Expand Up @@ -104,7 +104,7 @@ static Debug()
_consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Information);
_consoleLoggingLevelSwitch.MinimumLevelChanged += (sender, args) =>
{
Debug.Console(0, "Console debug level set to {0}", _consoleLoggingLevelSwitch.MinimumLevel);
Console(0, "Console debug level set to {0}", _consoleLoggingLevelSwitch.MinimumLevel);
};
_websocketLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Verbose);
_websocketSink = new DebugWebsocketSink(new JsonFormatter(renderMessage: true));
Expand All @@ -116,7 +116,7 @@ static Debug()
.WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch)
.WriteTo.File(@"\user\debug\global-log-{Date}.txt"
, rollingInterval: RollingInterval.Day
, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug)
, restrictedToMinimumLevel: LogEventLevel.Debug)
.CreateLogger();

// Get the assembly version and print it to console and the log
Expand Down Expand Up @@ -455,6 +455,7 @@ private static void LogMessage(uint level, string format, params object[] items)
if (!_logLevels.ContainsKey(level)) return;

var logLevel = _logLevels[level];

_logger.Write(logLevel, format, items);
}

Expand All @@ -464,8 +465,8 @@ private static void LogMessage(uint level, IKeyed keyed, string format, params o

var logLevel = _logLevels[level];

var log = _logger.ForContext("Key", keyed.Key);
log.Write(logLevel, format, items);
var logger = _logger.ForContext("Key", keyed.Key);
logger.Write(logLevel, format, items);
}


Expand Down
18 changes: 10 additions & 8 deletions src/Pepperdash Core/Logging/DebugConsoleSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ internal class DebugConsoleSink : ILogEventSink

public void Emit(LogEvent logEvent)
{
if (!Debug.IsRunningOnAppliance) return;
if (!Debug.IsRunningOnAppliance) return;

CrestronConsole.PrintLine("[{0}][App {1}][Lvl {2}]: {3}", logEvent.Timestamp,
InitialParametersClass.ApplicationNumber,
logEvent.Level,
logEvent.RenderMessage());
string message = $"[{logEvent.Timestamp}][{logEvent.Level}][App {InitialParametersClass.ApplicationNumber}]{logEvent.RenderMessage()}";

if(logEvent.Properties.TryGetValue("Key",out var value) && value is ScalarValue sv && sv.Value is string rawValue)
{
message = $"[{logEvent.Timestamp}][{logEvent.Level}][App {InitialParametersClass.ApplicationNumber}][{rawValue}]: {logEvent.RenderMessage()}";
}

CrestronConsole.PrintLine(message);
}

public DebugConsoleSink(ITextFormatter formatProvider)
public DebugConsoleSink(ITextFormatter formatProvider )
{

_textFormatter = formatProvider ?? new JsonFormatter();

}

}
Expand Down

0 comments on commit ada8b7e

Please sign in to comment.