diff --git a/EonaCat.Logger/EonaCat.Logger.csproj b/EonaCat.Logger/EonaCat.Logger.csproj
index ad0f40d..2275da1 100644
--- a/EonaCat.Logger/EonaCat.Logger.csproj
+++ b/EonaCat.Logger/EonaCat.Logger.csproj
@@ -3,7 +3,7 @@
.netstandard2.1; net6.0; net7.0; net8.0; net4.8;
icon.ico
latest
- 1.3.2
+ 1.3.3
EonaCat (Jeroen Saey)
true
EonaCat (Jeroen Saey)
@@ -24,7 +24,7 @@
- 1.3.2+{chash:10}.{c:ymd}
+ 1.3.3+{chash:10}.{c:ymd}
true
true
v[0-9]*
diff --git a/EonaCat.Logger/Extensions/ExceptionExtensions.cs b/EonaCat.Logger/Extensions/ExceptionExtensions.cs
index 3076f27..c254e77 100644
--- a/EonaCat.Logger/Extensions/ExceptionExtensions.cs
+++ b/EonaCat.Logger/Extensions/ExceptionExtensions.cs
@@ -63,10 +63,17 @@ public static class ExceptionExtensions
foreach (DictionaryEntry entry in data)
{
- sb.Append(" | ")
- .Append(entry.Key)
- .Append(": ")
- .AppendLine(entry.Value.ToString());
+ if (entry.Key != null)
+ {
+ sb.Append(" | ")
+ .Append(entry.Key);
+ }
+
+ if (entry.Value != null)
+ {
+ sb.Append(": ")
+ .AppendLine(entry.Value.ToString());
+ }
}
return sb.ToString();
diff --git a/EonaCat.Logger/Managers/LogHelper.cs b/EonaCat.Logger/Managers/LogHelper.cs
index 8393f25..a318e3e 100644
--- a/EonaCat.Logger/Managers/LogHelper.cs
+++ b/EonaCat.Logger/Managers/LogHelper.cs
@@ -23,7 +23,9 @@ public class ErrorMessage
internal static class LogHelper
{
- internal static event EventHandler OnException;
+ internal static event EventHandler OnException;
+
+ internal static event EventHandler OnLogLevelDisabled;
internal static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage,
DateTime dateTime, string category = null)
@@ -131,11 +133,27 @@ internal static class LogHelper
{
Log(logger, logLevel, message);
}
+ }
+
+ private bool IsLogLevelEnabled(LoggerSettings loggerSettings, ELogType logType)
+ {
+ if (loggerSettings == null)
+ {
+ OnLogLevelDisabled?.Invoke(this, new ErrorMessage { Message = "Settings is null." });
+ return false;
+ }
+
+ var isEnabled = loggerSettings.MaxLogType != ELogType.NONE && logType <= loggerSettings.MaxLogType;
+ if (!isEnabled)
+ {
+ OnLogLevelDisabled?.Invoke(this, new ErrorMessage { Message = $"Logtype '{logType}' is not enabled, cannot log message" });
+ }
+ return isEnabled;
}
- private static bool IsLogLevelEnabled(LoggerSettings settings, ELogType logType)
+ public static void SetLogLevel(LoggerSettings settings, ELogType logType)
{
- return settings.MaxLogType != ELogType.NONE && logType <= settings.MaxLogType;
+ settings.MaxLogType = logType;
}
private static void Log(ILogger logger, LogLevel logLevel, string message)
diff --git a/EonaCat.Logger/Managers/LogManager.cs b/EonaCat.Logger/Managers/LogManager.cs
index 3590fb4..e91790c 100644
--- a/EonaCat.Logger/Managers/LogManager.cs
+++ b/EonaCat.Logger/Managers/LogManager.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@@ -57,8 +56,14 @@ namespace EonaCat.Logger.Managers
SetupFileLogger(settings);
SetupLogManager();
LogHelper.OnException += LogHelper_OnException;
- }
-
+ LogHelper.OnLogLevelDisabled += LogHelper_OnLogLevelDisabled;
+ }
+
+ private void LogHelper_OnLogLevelDisabled(object sender, ErrorMessage e)
+ {
+ OnLogLevelDisabled?.Invoke(sender, e);
+ }
+
private DateTime CurrentDateTime => Settings.UseLocalTime ? DateTime.Now : DateTime.UtcNow;
public ILoggerProvider LoggerProvider { get; private set; }
public ILoggerFactory LoggerFactory { get; private set; }
@@ -111,8 +116,16 @@ namespace EonaCat.Logger.Managers
customSplunkSourceType, sendToGrayLogServers, grayLogFacility, grayLogSource, grayLogVersion);
}
+ ///
+ /// Gets fired when an exception occurs during logging
+ ///
public event EventHandler OnException;
+ ///
+ /// Gets fired when the log level is disabled and the user tries to log a message
+ ///
+ public event EventHandler OnLogLevelDisabled;
+
private static LoggerSettings CreateDefaultSettings()
{
var settings = new LoggerSettings
@@ -194,7 +207,7 @@ namespace EonaCat.Logger.Managers
string grayLogSource = null, string grayLogVersion = "1.1")
{
if (string.IsNullOrEmpty(message) || logType == ELogType.NONE ||
- (int)logType > (int)Settings.MaxLogType)
+ !IsLogLevelEnabled(logType))
{
return;
}
@@ -254,8 +267,24 @@ namespace EonaCat.Logger.Managers
};
Settings.OnLogEvent(logMessage);
- }
-
+ }
+
+ private bool IsLogLevelEnabled(ELogType logType)
+ {
+ if (Settings == null)
+ {
+ OnLogLevelDisabled?.Invoke(this, new ErrorMessage { Message = "Settings is null." });
+ return false;
+ }
+
+ var isEnabled = Settings.MaxLogType != ELogType.NONE && logType <= Settings.MaxLogType;
+ if (!isEnabled)
+ {
+ OnLogLevelDisabled?.Invoke(this, new ErrorMessage { Message = $"Logtype '{logType}' is not enabled, cannot log message" });
+ }
+ return isEnabled;
+ }
+
public void Reset()
{
Settings.ResetLogEvent();
diff --git a/README.md b/README.md
index 7385add..9a993e8 100644
--- a/README.md
+++ b/README.md
@@ -95,6 +95,7 @@ namespace EonaCat.Logger.Advanced
// Create and configure a LogManager for logging.
_logManager = new LogManager(new LoggerSettings { RemoveMessagePrefix = true });
_logManager.OnException += _logManager_OnException;
+ _logManager.OnLogLevelDisabled += _logManager_OnLogLevelDisabled;
_logManager.Settings.FileLoggerOptions.FileNamePrefix = "advanced";
_logManager.Settings.UseLocalTime = true;
_logManager.Settings.FileLoggerOptions.UseLocalTime = true;
@@ -106,6 +107,16 @@ namespace EonaCat.Logger.Advanced
// Event handler for LogManager exceptions, writes messages to the console.
private static void _logManager_OnException(object? sender, ErrorMessage e)
+ {
+ Console.WriteLine(e.Message);
+ if (e.Exception != null)
+ {
+ Console.WriteLine(e.Exception);
+ }
+ }
+
+ // Event handler for LogManager loglevel disabled notifications, writes messages to the console.
+ private static void _logManager_OnLogLevelDisabled(object? sender, ErrorMessage e)
{
Console.WriteLine(e.Message);
if (e.Exception != null)