From b98d8d49bab1ca23a7607f315521a189f5777c6d Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Thu, 21 Mar 2024 11:13:12 -0500 Subject: [PATCH] feat: add Errorlog sink --- src/Pepperdash Core/Logging/Debug.cs | 12 +++-- .../Logging/DebugErrorLogSink.cs | 44 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/Pepperdash Core/Logging/DebugErrorLogSink.cs diff --git a/src/Pepperdash Core/Logging/Debug.cs b/src/Pepperdash Core/Logging/Debug.cs index 5d46dde..b395e28 100644 --- a/src/Pepperdash Core/Logging/Debug.cs +++ b/src/Pepperdash Core/Logging/Debug.cs @@ -13,6 +13,7 @@ using Serilog.Formatting.Json; using Crestron.SimplSharp.CrestronDataStore; using PepperDash.Core.Logging; +using Serilog.Formatting.Compact; namespace PepperDash.Core { @@ -132,12 +133,12 @@ static Debug() .MinimumLevel.Verbose() .WriteTo.Sink(new DebugConsoleSink(new JsonFormatter(renderMessage: true)), levelSwitch: _consoleLoggingLevelSwitch) .WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch) - .WriteTo.File(logFilePath, - outputTemplate: "[{Timestamp}][{Level}][{Properties.Key}]{Message}{NewLine}", + .WriteTo.Sink(new DebugErrorLogSink(), LogEventLevel.Information) + .WriteTo.File(new CompactJsonFormatter(), logFilePath, rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Debug, retainedFileCountLimit: CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance ? 30 : 60 - ); ; + ); try { @@ -580,6 +581,7 @@ private static void LogMessage(uint level, IKeyed keyed, string format, params o /// /// Console format string /// Object parameters + [Obsolete("Use LogMessage methods")] public static void Console(uint level, string format, params object[] items) { @@ -605,6 +607,7 @@ public static void Console(uint level, string format, params object[] items) /// /// Logs to Console when at-level, and all messages to error log, including device key /// + [Obsolete("Use LogMessage methods")] public static void Console(uint level, IKeyed dev, string format, params object[] items) { LogMessage(level, dev, format, items); @@ -617,6 +620,7 @@ public static void Console(uint level, IKeyed dev, string format, params object[ /// Prints message to console if current debug level is equal to or higher than the level of this message. Always sends message to Error Log. /// Uses CrestronConsole.PrintLine. /// + [Obsolete("Use LogMessage methods")] public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel, string format, params object[] items) { @@ -643,6 +647,7 @@ public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel, /// /// Logs to Console when at-level, and all messages to error log /// + [Obsolete("Use LogMessage methods")] public static void Console(uint level, ErrorLogLevel errorLogLevel, string format, params object[] items) { @@ -694,6 +699,7 @@ public static void ConsoleWithLog(uint level, IKeyed dev, string format, params /// /// /// + [Obsolete("Use LogMessage methods")] public static void LogError(ErrorLogLevel errorLogLevel, string str) { diff --git a/src/Pepperdash Core/Logging/DebugErrorLogSink.cs b/src/Pepperdash Core/Logging/DebugErrorLogSink.cs new file mode 100644 index 0000000..b5bbb09 --- /dev/null +++ b/src/Pepperdash Core/Logging/DebugErrorLogSink.cs @@ -0,0 +1,44 @@ +using Crestron.SimplSharp; +using Serilog.Core; +using Serilog.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PepperDash.Core.Logging +{ + public class DebugErrorLogSink : ILogEventSink + { + private Dictionary> _errorLogMap = new Dictionary> + { + { LogEventLevel.Verbose, (msg) => ErrorLog.Notice(msg) }, + {LogEventLevel.Debug, (msg) => ErrorLog.Notice(msg) }, + {LogEventLevel.Information, (msg) => ErrorLog.Notice(msg) }, + {LogEventLevel.Warning, (msg) => ErrorLog.Warn(msg) }, + {LogEventLevel.Error, (msg) => ErrorLog.Error(msg) }, + {LogEventLevel.Fatal, (msg) => ErrorLog.Error(msg) } + }; + public void Emit(LogEvent logEvent) + { + var programId = CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance + ? $"App {InitialParametersClass.ApplicationNumber}" + : $"Room {InitialParametersClass.RoomId}"; + + string message = $"[{logEvent.Timestamp}][{logEvent.Level}][{programId}]{logEvent.RenderMessage()}"; + + if (logEvent.Properties.TryGetValue("Key", out var value) && value is ScalarValue sv && sv.Value is string rawValue) + { + message = $"[{logEvent.Timestamp}][{logEvent.Level}][{programId}][{rawValue}]: {logEvent.RenderMessage()}"; + } + + if(!_errorLogMap.TryGetValue(logEvent.Level, out var handler)) + { + return; + } + + handler(message); + } + } +}