Search code examples
.net-coreevent-logilogger

.Net Core ILogger saves Critical as Error


In my test POST action am saving 4 different logs (Information, Warning, Error & Critical) into Windows EventLog. I can see all four in Event viewer but one of them has wrong level. It is marker as 'Error' but it should be marked as 'critical'

enter image description here

Program.cs

return Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logger =>
                {
                    logger.ClearProviders();
                    logger.AddEventLog(new EventLogSettings
                    {
                        SourceName = "Website API",
                        LogName = "Website API"
                    });
                })...

appsettings.json

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }

Controller

public async Task<IActionResult> TestEndPoint()
        {
            _logger.LogInformation("LogInformation");
            _logger.LogWarning("LogWarning");
            _logger.LogError("LogError");
            _logger.LogCritical("LogCritical");
            return Ok(null);
        }

Application: REST API - .NET 5.0


Solution

  • How to log an Event log in the Critical level?
    How to create CRITICAL events for Windows Event Viewer?

    The critical log level in the EventLog is reserved for system/kernel stuff. There is no same-same mapping between EventLog's critical and .Net Core's critical, it's just mapped as error. It's unfortunate that they have the same name, but different meaning in these two contexts, which maybe confusing.