mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-02-16 05:04:48 +00:00
feat: adds console sink for Serilog
This commit is contained in:
@@ -85,7 +85,7 @@ namespace PepperDash.Core
|
|||||||
|
|
||||||
private const int SaveTimeoutMs = 30000;
|
private const int SaveTimeoutMs = 30000;
|
||||||
|
|
||||||
private static bool _runningOnAppliance = CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance;
|
public static bool IsRunningOnAppliance = CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Version for the currently loaded PepperDashCore dll
|
/// Version for the currently loaded PepperDashCore dll
|
||||||
@@ -108,12 +108,13 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
_consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Information);
|
_consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Information);
|
||||||
_websocketLoggingLevelSwitch = new LoggingLevelSwitch();
|
_websocketLoggingLevelSwitch = new LoggingLevelSwitch();
|
||||||
_websocketSink = new DebugWebsocketSink(new JsonFormatter());
|
_websocketSink = new DebugWebsocketSink(new JsonFormatter(renderMessage: true));
|
||||||
|
|
||||||
// Instantiate the root logger
|
// Instantiate the root logger
|
||||||
_logger = new LoggerConfiguration()
|
_logger = new LoggerConfiguration()
|
||||||
//.WriteTo.Logger(lc => lc
|
//.WriteTo.Logger(lc => lc
|
||||||
//.WriteTo.Console(levelSwitch: _consoleLoggingLevelSwitch))
|
//.WriteTo.Console(levelSwitch: _consoleLoggingLevelSwitch))
|
||||||
|
.WriteTo.Sink(new DebugConsoleSink(new JsonFormatter(renderMessage: true)), levelSwitch: _consoleLoggingLevelSwitch)
|
||||||
.WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch)
|
.WriteTo.Sink(_websocketSink, levelSwitch: _websocketLoggingLevelSwitch)
|
||||||
.WriteTo.File(@"\user\debug\global-log-{Date}.txt"
|
.WriteTo.File(@"\user\debug\global-log-{Date}.txt"
|
||||||
, rollingInterval: RollingInterval.Day
|
, rollingInterval: RollingInterval.Day
|
||||||
@@ -438,6 +439,18 @@ namespace PepperDash.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void LogMessage(uint level, string message)
|
||||||
|
{
|
||||||
|
_logger.Write((LogEventLevel)level, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void LogMessage(uint level, string message, IKeyed keyed)
|
||||||
|
{
|
||||||
|
var log = _logger.ForContext("Key", keyed.Key);
|
||||||
|
log.Write((LogEventLevel)level, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prints message to console if current debug level is equal to or higher than the level of this message.
|
/// Prints message to console if current debug level is equal to or higher than the level of this message.
|
||||||
/// Uses CrestronConsole.PrintLine.
|
/// Uses CrestronConsole.PrintLine.
|
||||||
@@ -459,13 +472,13 @@ namespace PepperDash.Core
|
|||||||
|
|
||||||
_logger.Write((LogEventLevel)level, message);
|
_logger.Write((LogEventLevel)level, message);
|
||||||
|
|
||||||
if (_runningOnAppliance)
|
//if (IsRunningOnAppliance)
|
||||||
{
|
//{
|
||||||
CrestronConsole.PrintLine("[{0}]App {1} Lvl {2}:{3}", DateTime.Now.ToString("HH:mm:ss.fff"),
|
// CrestronConsole.PrintLine("[{0}]App {1} Lvl {2}:{3}", DateTime.Now.ToString("HH:mm:ss.fff"),
|
||||||
InitialParametersClass.ApplicationNumber,
|
// InitialParametersClass.ApplicationNumber,
|
||||||
level,
|
// level,
|
||||||
string.Format(format, items));
|
// string.Format(format, items));
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -473,13 +486,10 @@ namespace PepperDash.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Console(uint level, IKeyed dev, string format, params object[] items)
|
public static void Console(uint level, IKeyed dev, string format, params object[] items)
|
||||||
{
|
{
|
||||||
var log = _logger.ForContext("Key", dev.Key);
|
LogMessage(level, string.Format(format, items), dev);
|
||||||
var message = string.Format(format, items);
|
|
||||||
|
|
||||||
log.Write((LogEventLevel)level, message);
|
//if (Level >= level)
|
||||||
|
// Console(level, "[{0}] {1}", dev.Key, message);
|
||||||
if (Level >= level)
|
|
||||||
Console(level, "[{0}] {1}", dev.Key, message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -496,15 +506,17 @@ namespace PepperDash.Core
|
|||||||
LogError(errorLogLevel, str);
|
LogError(errorLogLevel, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
var log = _logger.ForContext("Key", dev.Key);
|
LogMessage(level, str, dev);
|
||||||
var message = string.Format(format, items);
|
|
||||||
|
|
||||||
log.Write((LogEventLevel)level, message);
|
//var log = _logger.ForContext("Key", dev.Key);
|
||||||
|
//var message = string.Format(format, items);
|
||||||
|
|
||||||
if (Level >= level)
|
//log.Write((LogEventLevel)level, message);
|
||||||
{
|
|
||||||
Console(level, str);
|
//if (Level >= level)
|
||||||
}
|
//{
|
||||||
|
// Console(level, str);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -518,10 +530,12 @@ namespace PepperDash.Core
|
|||||||
{
|
{
|
||||||
LogError(errorLogLevel, str);
|
LogError(errorLogLevel, str);
|
||||||
}
|
}
|
||||||
if (Level >= level)
|
|
||||||
{
|
LogMessage(level, str);
|
||||||
Console(level, str);
|
//if (Level >= level)
|
||||||
}
|
//{
|
||||||
|
// Console(level, str);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -532,8 +546,9 @@ namespace PepperDash.Core
|
|||||||
public static void ConsoleWithLog(uint level, string format, params object[] items)
|
public static void ConsoleWithLog(uint level, string format, params object[] items)
|
||||||
{
|
{
|
||||||
var str = string.Format(format, items);
|
var str = string.Format(format, items);
|
||||||
if (Level >= level)
|
//if (Level >= level)
|
||||||
CrestronConsole.PrintLine("App {0}:{1}", InitialParametersClass.ApplicationNumber, str);
|
// CrestronConsole.PrintLine("App {0}:{1}", InitialParametersClass.ApplicationNumber, str);
|
||||||
|
LogMessage(level, str);
|
||||||
CrestronLogger.WriteToLog(str, level);
|
CrestronLogger.WriteToLog(str, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,8 +560,9 @@ namespace PepperDash.Core
|
|||||||
public static void ConsoleWithLog(uint level, IKeyed dev, string format, params object[] items)
|
public static void ConsoleWithLog(uint level, IKeyed dev, string format, params object[] items)
|
||||||
{
|
{
|
||||||
var str = string.Format(format, items);
|
var str = string.Format(format, items);
|
||||||
if (Level >= level)
|
|
||||||
ConsoleWithLog(level, "[{0}] {1}", dev.Key, str);
|
LogMessage(level, str, dev);
|
||||||
|
CrestronLogger.WriteToLog(string.Format("[{0}] {1}", dev.Key, str), level);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -557,7 +573,7 @@ namespace PepperDash.Core
|
|||||||
public static void LogError(ErrorLogLevel errorLogLevel, string str)
|
public static void LogError(ErrorLogLevel errorLogLevel, string str)
|
||||||
{
|
{
|
||||||
|
|
||||||
var msg = _runningOnAppliance ? string.Format("App {0}:{1}", InitialParametersClass.ApplicationNumber, str) : string.Format("Room {0}:{1}", InitialParametersClass.RoomId, str);
|
var msg = IsRunningOnAppliance ? string.Format("App {0}:{1}", InitialParametersClass.ApplicationNumber, str) : string.Format("Room {0}:{1}", InitialParametersClass.RoomId, str);
|
||||||
switch (errorLogLevel)
|
switch (errorLogLevel)
|
||||||
{
|
{
|
||||||
case ErrorLogLevel.Error:
|
case ErrorLogLevel.Error:
|
||||||
|
|||||||
50
src/Pepperdash Core/Logging/DebugConsoleSink.cs
Normal file
50
src/Pepperdash Core/Logging/DebugConsoleSink.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using Crestron.SimplSharp;
|
||||||
|
using Serilog.Configuration;
|
||||||
|
using Serilog;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
internal class DebugConsoleSink : ILogEventSink
|
||||||
|
{
|
||||||
|
private readonly ITextFormatter _textFormatter;
|
||||||
|
|
||||||
|
public void Emit(LogEvent logEvent)
|
||||||
|
{
|
||||||
|
if (!Debug.IsRunningOnAppliance) return;
|
||||||
|
|
||||||
|
CrestronConsole.PrintLine("[{0}]App {1} Lvl {2}:{3}", logEvent.Timestamp,
|
||||||
|
InitialParametersClass.ApplicationNumber,
|
||||||
|
logEvent.Level,
|
||||||
|
logEvent.RenderMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public DebugConsoleSink(ITextFormatter formatProvider)
|
||||||
|
{
|
||||||
|
|
||||||
|
_textFormatter = formatProvider ?? new JsonFormatter();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DebugConsoleSinkExtensions
|
||||||
|
{
|
||||||
|
public static LoggerConfiguration DebugConsoleSink(
|
||||||
|
this LoggerSinkConfiguration loggerConfiguration,
|
||||||
|
ITextFormatter formatProvider = null)
|
||||||
|
{
|
||||||
|
return loggerConfiguration.Sink(new DebugConsoleSink(formatProvider));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -157,37 +157,6 @@ namespace PepperDash.Core
|
|||||||
};
|
};
|
||||||
Debug.Console(0, "Starting");
|
Debug.Console(0, "Starting");
|
||||||
|
|
||||||
//_httpsServer.OnGet += (sender, e) =>
|
|
||||||
//{
|
|
||||||
// Debug.Console(0, $"OnGet requesting {e.Request}");
|
|
||||||
// var req = e.Request;
|
|
||||||
// var res = e.Response;
|
|
||||||
|
|
||||||
// var path = req.RawUrl;
|
|
||||||
|
|
||||||
// if (path == "/")
|
|
||||||
// path += "index.html";
|
|
||||||
|
|
||||||
// var localPath = Path.Combine(rootPath, path.Substring(1));
|
|
||||||
|
|
||||||
// byte[] contents;
|
|
||||||
// if (File.Exists(localPath))
|
|
||||||
// contents = File.ReadAllBytes(localPath);
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// e.Response.StatusCode = 404;
|
|
||||||
// contents = Encoding.UTF8.GetBytes("Path not found " + e.Request.RawUrl);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var extention = Path.GetExtension(path);
|
|
||||||
// if (!_contentTypes.TryGetValue(extention, out var contentType))
|
|
||||||
// contentType = "text/html";
|
|
||||||
|
|
||||||
// res.ContentLength64 = contents.LongLength;
|
|
||||||
|
|
||||||
// res.Close(contents, true);
|
|
||||||
//};
|
|
||||||
|
|
||||||
_httpsServer.Start();
|
_httpsServer.Start();
|
||||||
Debug.Console(0, "Ready");
|
Debug.Console(0, "Ready");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user