Add LogLevel Blacklist

This commit is contained in:
Mira 2022-06-06 04:35:42 +02:00
parent f0e3ccc41b
commit 24dc68b308
Signed by untrusted user who does not match committer: Xorog
GPG key ID: 983798ED9C3E7C36
2 changed files with 20 additions and 6 deletions

View file

@ -134,11 +134,14 @@ public class Logger : ILogger
try try
{ {
Byte[] FileWrite = Encoding.UTF8.GetBytes($"[{currentLog.TimeOfEvent:dd.MM.yyyy HH:mm:ss}] [{LogLevelText}] {LogMessage}\n{(currentLog.Exception is not null ? $"{currentLog.Exception}\n" : "")}"); if (!_loggerObjects.FileBlackList.Contains(currentLog.LogLevel))
if (OpenedFile != null)
{ {
await OpenedFile.WriteAsync(FileWrite.AsMemory(0, FileWrite.Length)); Byte[] FileWrite = Encoding.UTF8.GetBytes($"[{currentLog.TimeOfEvent:dd.MM.yyyy HH:mm:ss}] [{LogLevelText}] {LogMessage}\n{(currentLog.Exception is not null ? $"{currentLog.Exception}\n" : "")}");
OpenedFile.Flush(); if (OpenedFile != null)
{
await OpenedFile.WriteAsync(FileWrite.AsMemory(0, FileWrite.Length));
OpenedFile.Flush();
}
} }
} }
catch (Exception ex) catch (Exception ex)
@ -192,6 +195,15 @@ public class Logger : ILogger
_loggerObjects.Blacklist.Add(blacklist); _loggerObjects.Blacklist.Add(blacklist);
} }
/// <summary>
/// Add blacklisted log level to not save
/// </summary>
/// <param name="blacklist"></param>
public static void AddLogLevelBlacklist(LoggerObjects.LogLevel level)
{
_loggerObjects.FileBlackList.Add(level);
}
/// <summary> /// <summary>
@ -365,7 +377,7 @@ public class Logger : ILogger
LogLevel = logLevel switch LogLevel = logLevel switch
{ {
Microsoft.Extensions.Logging.LogLevel.Debug => LoggerObjects.LogLevel.DEBUG2, Microsoft.Extensions.Logging.LogLevel.Debug => LoggerObjects.LogLevel.DEBUG2,
Microsoft.Extensions.Logging.LogLevel.Trace => LoggerObjects.LogLevel.TRACE, Microsoft.Extensions.Logging.LogLevel.Trace => LoggerObjects.LogLevel.TRACE2,
Microsoft.Extensions.Logging.LogLevel.Information => LoggerObjects.LogLevel.INFO, Microsoft.Extensions.Logging.LogLevel.Information => LoggerObjects.LogLevel.INFO,
Microsoft.Extensions.Logging.LogLevel.Warning => LoggerObjects.LogLevel.WARN, Microsoft.Extensions.Logging.LogLevel.Warning => LoggerObjects.LogLevel.WARN,
Microsoft.Extensions.Logging.LogLevel.Error => LoggerObjects.LogLevel.ERROR, Microsoft.Extensions.Logging.LogLevel.Error => LoggerObjects.LogLevel.ERROR,

View file

@ -4,6 +4,7 @@ public class LoggerObjects
{ {
internal List<LogEntry> LogsToPost = new(); internal List<LogEntry> LogsToPost = new();
internal List<string> Blacklist = new(); internal List<string> Blacklist = new();
internal List<LogLevel> FileBlackList = new();
public class LogEntry public class LogEntry
{ {
@ -22,6 +23,7 @@ public class LoggerObjects
INFO, INFO,
DEBUG, DEBUG,
DEBUG2, DEBUG2,
TRACE TRACE,
TRACE2
} }
} }