Search code examples
asp.net-coreloggingevent-log

Logging LogLevel-Information not showing in Windows event log



I ran into some problems with logging **Information** Logs to Windows event log.
Starting from a blank ASP.NET Core-Web-API .Net 5

I edited the following to Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddEventLog(eventLogSettings =>
            {
                eventLogSettings.SourceName = "MyTestLog";
            });

        });
        webBuilder.UseStartup<Startup>();
    });

and wrote some sample logs into a Controler

public IEnumerable<WeatherForecast> Get()
{
    _logger.LogCritical("Critical Log");
    _logger.LogError("Error Log");
    _logger.LogWarning("Warning Log");
    _logger.LogInformation("Info Log");
    _logger.LogDebug("Debug Log");
    _logger.LogTrace("Trace Log");
    ...
}

If I Run it, it shows logs for Critical - Error - Warning - Information in Console. Matching what is configured in AppSettings.
appsettings.json:

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

In Windows event log it only shows Critical, Error and Warning -Logs, not caring for my appsettings whatsoever. I'm sure this is an easy configuration problem, but i don't find any documentation for this.

How do I get Logs with Loglevel-Information in Windows event log ?


Solution

  • According to this article, you could find the ASP.NET Core provide the event logging feature. Unlike the other providers, the EventLog provider does not inherit the default non-provider settings.

    If EventLog log settings aren't specified, they default to LogLevel.Warning.

    To log events lower than LogLevel.Warning, explicitly set the log level. The following example sets the Event Log default log level to LogLevel.Information:

    "Logging": {
      "EventLog": {
        "LogLevel": {
          "Default": "Information"
        }
      }
    }