mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-01-11 19:44:44 +00:00
fix: debug all devices when devices array is empty
This commit is contained in:
@@ -1,69 +1,81 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Linq;
|
||||||
using Crestron.SimplSharp;
|
using System.Text;
|
||||||
using Serilog;
|
using Crestron.SimplSharp;
|
||||||
using Serilog.Configuration;
|
using Serilog;
|
||||||
using Serilog.Core;
|
using Serilog.Configuration;
|
||||||
using Serilog.Events;
|
using Serilog.Core;
|
||||||
using Serilog.Formatting;
|
using Serilog.Events;
|
||||||
using Serilog.Formatting.Json;
|
using Serilog.Formatting;
|
||||||
|
using Serilog.Formatting.Json;
|
||||||
namespace PepperDash.Core.Logging
|
|
||||||
{
|
namespace PepperDash.Core.Logging
|
||||||
public class DebugConsoleSink : ILogEventSink
|
{
|
||||||
{
|
public class DebugConsoleSink : ILogEventSink
|
||||||
private readonly ITextFormatter textFormatter;
|
{
|
||||||
|
private readonly ITextFormatter textFormatter;
|
||||||
public void Emit(LogEvent logEvent)
|
|
||||||
{
|
public void Emit(LogEvent logEvent)
|
||||||
|
{
|
||||||
/*string message = $"[{logEvent.Timestamp}][{logEvent.Level}][App {InitialParametersClass.ApplicationNumber}]{logEvent.RenderMessage()}";
|
|
||||||
|
/*string message = $"[{logEvent.Timestamp}][{logEvent.Level}][App {InitialParametersClass.ApplicationNumber}]{logEvent.RenderMessage()}";
|
||||||
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,3}]: {logEvent.RenderMessage()}";
|
{
|
||||||
}*/
|
message = $"[{logEvent.Timestamp}][{logEvent.Level}][App {InitialParametersClass.ApplicationNumber}][{rawValue,3}]: {logEvent.RenderMessage()}";
|
||||||
|
}*/
|
||||||
var buffer = new StringWriter(new StringBuilder(256));
|
|
||||||
|
var buffer = new StringWriter(new StringBuilder(256));
|
||||||
textFormatter.Format(logEvent, buffer);
|
|
||||||
|
textFormatter.Format(logEvent, buffer);
|
||||||
var message = buffer.ToString();
|
|
||||||
|
var message = buffer.ToString();
|
||||||
CrestronConsole.PrintLine(message);
|
|
||||||
}
|
CrestronConsole.PrintLine(message);
|
||||||
|
}
|
||||||
public DebugConsoleSink(ITextFormatter formatProvider)
|
|
||||||
{
|
public DebugConsoleSink(ITextFormatter formatProvider)
|
||||||
textFormatter = formatProvider ?? new JsonFormatter();
|
{
|
||||||
}
|
textFormatter = formatProvider ?? new JsonFormatter();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public static class DebugConsoleSinkExtensions
|
|
||||||
{
|
public static class DebugConsoleSinkExtensions
|
||||||
public static LoggerConfiguration DebugConsoleSink(
|
{
|
||||||
this LoggerSinkConfiguration loggerConfiguration,
|
public static LoggerConfiguration DebugConsoleSink(
|
||||||
ITextFormatter formatProvider = null,
|
this LoggerSinkConfiguration loggerConfiguration,
|
||||||
LoggingLevelSwitch levelSwitch = null)
|
ITextFormatter formatProvider = null,
|
||||||
{
|
LoggingLevelSwitch levelSwitch = null)
|
||||||
var sink = new DebugConsoleSink(formatProvider);
|
{
|
||||||
return loggerConfiguration.Conditional(Predicate, c => c.Sink(sink, levelSwitch: levelSwitch));
|
var sink = new DebugConsoleSink(formatProvider);
|
||||||
|
return loggerConfiguration.Conditional(Predicate, c => c.Sink(sink, levelSwitch: levelSwitch));
|
||||||
static bool Predicate(LogEvent @event)
|
|
||||||
{
|
static bool Predicate(LogEvent @event)
|
||||||
if (!Debug.IsRunningOnAppliance)
|
{
|
||||||
{
|
if (!Debug.IsRunningOnAppliance)
|
||||||
return false;
|
{
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
if (@event.Properties.TryGetValue("Key", out var value) &&
|
|
||||||
value is ScalarValue { Value: string rawValue })
|
if (@event.Properties.TryGetValue("Key", out var value) && value is ScalarValue { Value: string rawValue }
|
||||||
{
|
&& DebugContext.TryGetDataForKey(Debug.ConsoleLevelStoreKey, out var data)
|
||||||
return DebugContext.DeviceExistsInContext(Debug.ConsoleLevelStoreKey, rawValue);
|
&& data.Devices != null)
|
||||||
}
|
{
|
||||||
|
if (data.Devices.Length == 0)
|
||||||
return true;
|
{
|
||||||
}
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
if (data.Devices.Any(d => d == rawValue))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,110 +1,107 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Crestron.SimplSharp;
|
using Crestron.SimplSharp;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
|
|
||||||
namespace PepperDash.Core.Logging
|
namespace PepperDash.Core.Logging
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a debugging context
|
/// Represents a debugging context
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class DebugContext
|
public static class DebugContext
|
||||||
{
|
{
|
||||||
private static readonly CTimer SaveTimer;
|
private static readonly CTimer SaveTimer;
|
||||||
private static readonly Dictionary<string, DebugContextData> CurrentData;
|
private static readonly Dictionary<string, DebugContextData> CurrentData;
|
||||||
|
|
||||||
public static readonly string ApplianceFilePath = Path.Combine("/", "user", "debug", $"app{InitialParametersClass.ApplicationNumber.ToString().PadLeft(2, '0')}-debug.json");
|
public static readonly string ApplianceFilePath = Path.Combine("/", "user", "debug", $"app{InitialParametersClass.ApplicationNumber.ToString().PadLeft(2, '0')}-debug.json");
|
||||||
public static readonly string ServerFilePath = Path.Combine("/", "User", "debug", $"{InitialParametersClass.RoomId}-debug.json");
|
public static readonly string ServerFilePath = Path.Combine("/", "User", "debug", $"{InitialParametersClass.RoomId}-debug.json");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The name of the file containing the current debug settings.
|
/// The name of the file containing the current debug settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly string FileName = CrestronEnvironment.DevicePlatform switch
|
public static readonly string FileName = CrestronEnvironment.DevicePlatform switch
|
||||||
{
|
{
|
||||||
eDevicePlatform.Appliance => ApplianceFilePath,
|
eDevicePlatform.Appliance => ApplianceFilePath,
|
||||||
eDevicePlatform.Server => ServerFilePath,
|
eDevicePlatform.Server => ServerFilePath,
|
||||||
_ => string.Empty
|
_ => string.Empty
|
||||||
};
|
};
|
||||||
|
|
||||||
static DebugContext()
|
static DebugContext()
|
||||||
{
|
{
|
||||||
CurrentData = LoadData();
|
CurrentData = LoadData();
|
||||||
SaveTimer = new CTimer(_ => SaveData(), Timeout.Infinite);
|
SaveTimer = new CTimer(_ => SaveData(), Timeout.Infinite);
|
||||||
|
|
||||||
CrestronEnvironment.ProgramStatusEventHandler += args =>
|
CrestronEnvironment.ProgramStatusEventHandler += args =>
|
||||||
{
|
{
|
||||||
if (args == eProgramStatusEventType.Stopping)
|
if (args == eProgramStatusEventType.Stopping)
|
||||||
{
|
{
|
||||||
using (SaveTimer)
|
using (SaveTimer)
|
||||||
{
|
{
|
||||||
SaveData();
|
SaveData();
|
||||||
SaveTimer.Stop();
|
SaveTimer.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DebugContextData GetDataForKey(string key, LogEventLevel defaultLevel) =>
|
public static bool TryGetDataForKey(string key, out DebugContextData data) =>
|
||||||
CurrentData.TryGetValue(key, out var data) ? data : new DebugContextData(defaultLevel);
|
CurrentData.TryGetValue(key, out var data);
|
||||||
|
|
||||||
public static bool DeviceExistsInContext(string contextKey, string deviceKey) =>
|
public static DebugContextData GetOrCreateDataForKey(string key, LogEventLevel defaultLevel) =>
|
||||||
CurrentData.TryGetValue(contextKey, out var data) switch
|
CurrentData.TryGetValue(key, out var data) ? data : new DebugContextData(defaultLevel);
|
||||||
{
|
|
||||||
true when data.Devices != null => data.Devices.Any(key => string.Equals(key, deviceKey, StringComparison.OrdinalIgnoreCase)),
|
|
||||||
_ => false
|
public static void SetDataForKey(string key, LogEventLevel defaultLevel, string[] devices = null)
|
||||||
};
|
{
|
||||||
|
if (CurrentData.ContainsKey(key))
|
||||||
public static void SetDataForKey(string key, LogEventLevel defaultLevel, string[] devices = null)
|
{
|
||||||
{
|
CurrentData[key] = new DebugContextData(defaultLevel, devices);
|
||||||
if (CurrentData.ContainsKey(key))
|
}
|
||||||
{
|
else
|
||||||
CurrentData[key] = new DebugContextData(defaultLevel, devices);
|
{
|
||||||
}
|
CurrentData.Add(key, new DebugContextData(defaultLevel, devices));
|
||||||
else
|
}
|
||||||
{
|
|
||||||
CurrentData.Add(key, new DebugContextData(defaultLevel, devices));
|
SaveTimer.Reset(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
SaveTimer.Reset(5000);
|
private static void SaveData()
|
||||||
}
|
{
|
||||||
|
Log.Information("Saving debug data to file:{File}", FileName);
|
||||||
private static void SaveData()
|
|
||||||
{
|
try
|
||||||
Log.Information("Saving debug data to file:{File}", FileName);
|
{
|
||||||
|
var debugDirectory = Path.GetDirectoryName(FileName) ??
|
||||||
try
|
throw new Exception("File directory is root");
|
||||||
{
|
|
||||||
var debugDirectory = Path.GetDirectoryName(FileName) ??
|
Directory.CreateDirectory(debugDirectory);
|
||||||
throw new Exception("File directory is root");
|
var json = JsonConvert.SerializeObject(CurrentData);
|
||||||
|
File.WriteAllText(FileName, json);
|
||||||
Directory.CreateDirectory(debugDirectory);
|
}
|
||||||
var json = JsonConvert.SerializeObject(CurrentData);
|
catch (Exception e)
|
||||||
File.WriteAllText(FileName, json);
|
{
|
||||||
}
|
Log.Error(e, "Failed to save debug data");
|
||||||
catch (Exception e)
|
}
|
||||||
{
|
}
|
||||||
Log.Error(e, "Failed to save debug data");
|
|
||||||
}
|
private static Dictionary<string, DebugContextData> LoadData()
|
||||||
}
|
{
|
||||||
|
if (!File.Exists(FileName))
|
||||||
private static Dictionary<string, DebugContextData> LoadData()
|
{
|
||||||
{
|
return new Dictionary<string, DebugContextData>();
|
||||||
if (!File.Exists(FileName))
|
}
|
||||||
{
|
|
||||||
return new Dictionary<string, DebugContextData>();
|
var json = File.ReadAllText(FileName);
|
||||||
}
|
return JsonConvert.DeserializeObject<Dictionary<string, DebugContextData>>(json);
|
||||||
|
}
|
||||||
var json = File.ReadAllText(FileName);
|
}
|
||||||
return JsonConvert.DeserializeObject<Dictionary<string, DebugContextData>>(json);
|
|
||||||
}
|
/// <summary>
|
||||||
}
|
///
|
||||||
|
/// </summary>
|
||||||
/// <summary>
|
public record DebugContextData(LogEventLevel Level, string[] Devices = null);
|
||||||
///
|
}
|
||||||
/// </summary>
|
|
||||||
public record DebugContextData(LogEventLevel Level, string[] Devices = null);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user