mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-01-11 19:44:44 +00:00
feat: add method to log Exceptions using default Serilog options
Alos modified console sink to pad non-keyed messages with 3 characters of empty space
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
using Serilog.Sinks.SystemConsole;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace PepperDash.Core
|
||||
{
|
||||
@@ -53,7 +50,7 @@ namespace PepperDash.Core
|
||||
public Device(string key)
|
||||
{
|
||||
Key = key;
|
||||
if (key.Contains('.')) Debug.Console(0, this, "WARNING: Device name's should not include '.'");
|
||||
if (key.Contains('.')) Debug.LogMessage(LogEventLevel.Information, "WARNING: Device key should not include '.'", this);
|
||||
Name = "";
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@ using Serilog.Formatting.Json;
|
||||
using Crestron.SimplSharp.CrestronDataStore;
|
||||
using PepperDash.Core.Logging;
|
||||
using Serilog.Formatting.Compact;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Diagnostics;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace PepperDash.Core
|
||||
{
|
||||
@@ -35,7 +38,7 @@ namespace PepperDash.Core
|
||||
{2, LogEventLevel.Verbose },
|
||||
};
|
||||
|
||||
private static Logger _logger;
|
||||
private static ILogger _logger;
|
||||
|
||||
private static readonly LoggingLevelSwitch _consoleLoggingLevelSwitch;
|
||||
|
||||
@@ -131,6 +134,7 @@ namespace PepperDash.Core
|
||||
|
||||
_defaultLoggerConfiguration = new LoggerConfiguration()
|
||||
.MinimumLevel.Verbose()
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Sink(new DebugConsoleSink(new JsonFormatter(renderMessage: true)), levelSwitch: _consoleLoggingLevelSwitch)
|
||||
.WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch)
|
||||
.WriteTo.Sink(new DebugErrorLogSink(), LogEventLevel.Information)
|
||||
@@ -542,16 +546,46 @@ namespace PepperDash.Core
|
||||
CrestronConsole.ConsoleCommandResponse(l + CrestronEnvironment.NewLine);
|
||||
}
|
||||
|
||||
public static void LogMessage(LogEventLevel level, string message, params object[] args)
|
||||
/// <summary>
|
||||
/// Log an Exception using Serilog's default Exception logging mechanism
|
||||
/// </summary>
|
||||
/// <param name="ex">Exception to log</param>
|
||||
/// <param name="message">Message template</param>
|
||||
/// <param name="device">Optional IKeyed device. If provided, the Key of the device will be added to the log message</param>
|
||||
/// <param name="args">Args to put into message template</param>
|
||||
public static void LogMessage(Exception ex, string message, IKeyed device = null, params object[] args)
|
||||
{
|
||||
_logger.Write(level, message, args);
|
||||
using (LogContext.PushProperty("Key", device?.Key ?? string.Empty))
|
||||
{
|
||||
_logger.Error(ex, message, args);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Log a message
|
||||
/// </summary>
|
||||
/// <param name="level">Level to log at</param>
|
||||
/// <param name="message">Message template</param>
|
||||
/// <param name="device">Optional IKeyed device. If provided, the Key of the device will be added to the log message</param>
|
||||
/// <param name="args">Args to put into message template</param>
|
||||
public static void LogMessage(LogEventLevel level, string message, IKeyed device=null, params object[] args)
|
||||
{
|
||||
using (LogContext.PushProperty("Key", device?.Key ?? string.Empty))
|
||||
{
|
||||
_logger.Write(level, message, args);
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use overload with optional IKeyed parameter")]
|
||||
public static void LogMessage(LogEventLevel level, string message, params object[] args)
|
||||
{
|
||||
LogMessage(level, message, null, args);
|
||||
}
|
||||
|
||||
[Obsolete("Use overload with optional IKeyed parameter")]
|
||||
public static void LogMessage(LogEventLevel level, IKeyed keyed, string message, params object[] args)
|
||||
{
|
||||
var log = _logger.ForContext("Key", keyed.Key);
|
||||
|
||||
log.Write(level, message, args);
|
||||
LogMessage(level, message, keyed, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -561,7 +595,7 @@ namespace PepperDash.Core
|
||||
|
||||
var logLevel = _logLevels[level];
|
||||
|
||||
LogMessage(logLevel, format, items );
|
||||
LogMessage(logLevel, format, items);
|
||||
}
|
||||
|
||||
private static void LogMessage(uint level, IKeyed keyed, string format, params object[] items)
|
||||
|
||||
@@ -5,12 +5,7 @@ using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
using Serilog.Formatting;
|
||||
using Serilog.Formatting.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace PepperDash.Core
|
||||
{
|
||||
@@ -26,7 +21,7 @@ namespace PepperDash.Core
|
||||
|
||||
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()}";
|
||||
message = $"[{logEvent.Timestamp}][{logEvent.Level}][App {InitialParametersClass.ApplicationNumber}][{rawValue,3}]: {logEvent.RenderMessage()}";
|
||||
}
|
||||
|
||||
CrestronConsole.PrintLine(message);
|
||||
|
||||
Reference in New Issue
Block a user