Skip to content

Commit

Permalink
feat: add Errorlog sink
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-welker committed Mar 21, 2024
1 parent 8403265 commit b98d8d4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Pepperdash Core/Logging/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Serilog.Formatting.Json;
using Crestron.SimplSharp.CrestronDataStore;
using PepperDash.Core.Logging;
using Serilog.Formatting.Compact;

namespace PepperDash.Core
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -580,6 +581,7 @@ private static void LogMessage(uint level, IKeyed keyed, string format, params o
/// <param name="level"></param>
/// <param name="format">Console format string</param>
/// <param name="items">Object parameters</param>
[Obsolete("Use LogMessage methods")]
public static void Console(uint level, string format, params object[] items)
{

Expand All @@ -605,6 +607,7 @@ public static void Console(uint level, string format, params object[] items)
/// <summary>
/// Logs to Console when at-level, and all messages to error log, including device key
/// </summary>
[Obsolete("Use LogMessage methods")]
public static void Console(uint level, IKeyed dev, string format, params object[] items)
{
LogMessage(level, dev, format, items);
Expand All @@ -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.
/// </summary>
[Obsolete("Use LogMessage methods")]
public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel,
string format, params object[] items)
{
Expand All @@ -643,6 +647,7 @@ public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel,
/// <summary>
/// Logs to Console when at-level, and all messages to error log
/// </summary>
[Obsolete("Use LogMessage methods")]
public static void Console(uint level, ErrorLogLevel errorLogLevel,
string format, params object[] items)
{
Expand Down Expand Up @@ -694,6 +699,7 @@ public static void ConsoleWithLog(uint level, IKeyed dev, string format, params
/// </summary>
/// <param name="errorLogLevel"></param>
/// <param name="str"></param>
[Obsolete("Use LogMessage methods")]
public static void LogError(ErrorLogLevel errorLogLevel, string str)
{

Expand Down
44 changes: 44 additions & 0 deletions src/Pepperdash Core/Logging/DebugErrorLogSink.cs
Original file line number Diff line number Diff line change
@@ -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<LogEventLevel, Action<string>> _errorLogMap = new Dictionary<LogEventLevel, Action<string>>
{
{ 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);
}
}
}

0 comments on commit b98d8d4

Please sign in to comment.