mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-02-15 12:44:48 +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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using Serilog.Events;
|
||||||
using Serilog;
|
|
||||||
using Serilog.Core;
|
|
||||||
using Serilog.Sinks.SystemConsole;
|
|
||||||
|
|
||||||
namespace PepperDash.Core
|
namespace PepperDash.Core
|
||||||
{
|
{
|
||||||
@@ -53,7 +50,7 @@ namespace PepperDash.Core
|
|||||||
public Device(string key)
|
public Device(string key)
|
||||||
{
|
{
|
||||||
Key = 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 = "";
|
Name = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ using Serilog.Formatting.Json;
|
|||||||
using Crestron.SimplSharp.CrestronDataStore;
|
using Crestron.SimplSharp.CrestronDataStore;
|
||||||
using PepperDash.Core.Logging;
|
using PepperDash.Core.Logging;
|
||||||
using Serilog.Formatting.Compact;
|
using Serilog.Formatting.Compact;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Serilog.Context;
|
||||||
|
|
||||||
namespace PepperDash.Core
|
namespace PepperDash.Core
|
||||||
{
|
{
|
||||||
@@ -35,7 +38,7 @@ namespace PepperDash.Core
|
|||||||
{2, LogEventLevel.Verbose },
|
{2, LogEventLevel.Verbose },
|
||||||
};
|
};
|
||||||
|
|
||||||
private static Logger _logger;
|
private static ILogger _logger;
|
||||||
|
|
||||||
private static readonly LoggingLevelSwitch _consoleLoggingLevelSwitch;
|
private static readonly LoggingLevelSwitch _consoleLoggingLevelSwitch;
|
||||||
|
|
||||||
@@ -131,6 +134,7 @@ namespace PepperDash.Core
|
|||||||
|
|
||||||
_defaultLoggerConfiguration = new LoggerConfiguration()
|
_defaultLoggerConfiguration = new LoggerConfiguration()
|
||||||
.MinimumLevel.Verbose()
|
.MinimumLevel.Verbose()
|
||||||
|
.Enrich.FromLogContext()
|
||||||
.WriteTo.Sink(new DebugConsoleSink(new JsonFormatter(renderMessage: true)), levelSwitch: _consoleLoggingLevelSwitch)
|
.WriteTo.Sink(new DebugConsoleSink(new JsonFormatter(renderMessage: true)), levelSwitch: _consoleLoggingLevelSwitch)
|
||||||
.WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch)
|
.WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch)
|
||||||
.WriteTo.Sink(new DebugErrorLogSink(), LogEventLevel.Information)
|
.WriteTo.Sink(new DebugErrorLogSink(), LogEventLevel.Information)
|
||||||
@@ -542,16 +546,46 @@ namespace PepperDash.Core
|
|||||||
CrestronConsole.ConsoleCommandResponse(l + CrestronEnvironment.NewLine);
|
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)
|
||||||
|
{
|
||||||
|
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);
|
_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)
|
public static void LogMessage(LogEventLevel level, IKeyed keyed, string message, params object[] args)
|
||||||
{
|
{
|
||||||
var log = _logger.ForContext("Key", keyed.Key);
|
LogMessage(level, message, keyed, args);
|
||||||
|
|
||||||
log.Write(level, message, args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,7 @@ using Serilog.Core;
|
|||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Serilog.Formatting;
|
using Serilog.Formatting;
|
||||||
using Serilog.Formatting.Json;
|
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
|
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)
|
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);
|
CrestronConsole.PrintLine(message);
|
||||||
|
|||||||
Reference in New Issue
Block a user