feat: add public LogMessage methods

Also add logic to allow using string like `Information` with `appdebug` console command.
This commit is contained in:
Andrew Welker
2024-03-20 10:24:27 -05:00
parent 32caa005a3
commit b41f4190be

View File

@@ -280,10 +280,24 @@ namespace PepperDash.Core
return; return;
} }
var level = Convert.ToUInt32(levelString); if(int.TryParse(levelString, out var levelInt))
{
if(levelInt < 0 || levelInt > 5)
{
CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level. If using a number, value must be between 0-5");
return;
}
SetDebugLevel((LogEventLevel)levelInt);
return;
}
if (_logLevels.ContainsKey(level)) if(Enum.TryParse<LogEventLevel>(levelString, out var levelEnum))
SetDebugLevel(level); {
SetDebugLevel(levelEnum);
return;
}
CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level");
} }
catch catch
{ {
@@ -297,15 +311,28 @@ namespace PepperDash.Core
/// <param name="level"> Valid values 0-5</param> /// <param name="level"> Valid values 0-5</param>
public static void SetDebugLevel(uint level) public static void SetDebugLevel(uint level)
{ {
if (_logLevels.ContainsKey(level)) if(!_logLevels.TryGetValue(level, out var logLevel))
_consoleLoggingLevelSwitch.MinimumLevel = _logLevels[level]; {
logLevel = LogEventLevel.Information;
CrestronConsole.PrintLine($"{level} not valid. Setting level to {logLevel}");
SetDebugLevel(logLevel);
}
SetDebugLevel(logLevel);
}
public static void SetDebugLevel(LogEventLevel level)
{
_consoleLoggingLevelSwitch.MinimumLevel = level;
CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}", CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}",
InitialParametersClass.ApplicationNumber, _consoleLoggingLevelSwitch.MinimumLevel); InitialParametersClass.ApplicationNumber, _consoleLoggingLevelSwitch.MinimumLevel);
var err = CrestronDataStoreStatic.SetLocalUintValue("ConsoleDebugLevel", level); var err = CrestronDataStoreStatic.SetLocalUintValue("ConsoleDebugLevel", (uint) level);
if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS)
CrestronConsole.PrintLine("Error saving console debug level setting: {0}", err); CrestronConsole.PrintLine($"Error saving console debug level setting: {err}");
} }
public static void SetWebSocketMinimumDebugLevel(LogEventLevel level) public static void SetWebSocketMinimumDebugLevel(LogEventLevel level)
@@ -470,6 +497,18 @@ namespace PepperDash.Core
CrestronConsole.ConsoleCommandResponse(l + CrestronEnvironment.NewLine); CrestronConsole.ConsoleCommandResponse(l + CrestronEnvironment.NewLine);
} }
public static void LogMessage(LogEventLevel level, string message, params object[] args)
{
_logger.Write(level, message, args);
}
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);
}
private static void LogMessage(uint level, string format, params object[] items) private static void LogMessage(uint level, string format, params object[] items)
{ {
@@ -477,7 +516,7 @@ namespace PepperDash.Core
var logLevel = _logLevels[level]; var logLevel = _logLevels[level];
_logger.Write(logLevel, format, items); LogMessage(logLevel, format, items );
} }
private static void LogMessage(uint level, IKeyed keyed, string format, params object[] items) private static void LogMessage(uint level, IKeyed keyed, string format, params object[] items)
@@ -486,8 +525,7 @@ namespace PepperDash.Core
var logLevel = _logLevels[level]; var logLevel = _logLevels[level];
var logger = _logger.ForContext("Key", keyed.Key); LogMessage(logLevel, keyed, format, items);
logger.Write(logLevel, format, items);
} }