Logging LogLevel-Information not showing in Windows event log [duplicate]
Asked Answered
J

1

9

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 ?

Jeth answered 28/2, 2022 at 7:51 Comment(1)
See my answer hereJeffjeffcoat
L
16

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"
    }
  }
}
Lanthanum answered 1/3, 2022 at 2:22 Comment(1)
I put this Logging setup in my appsettings.json: "Logging": { "EventLog": { "LogLevel": { "Default": "Information" } } } But the application would not log the Information for the Controller activity. When I deleted appSettings.json alltogether, it would log everything , like this: info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/1.1 GET localhost:5232/api/test - - info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] Executing endpoint 'WebAPIBareboneDotNet6.Controllers .....Illgotten

© 2022 - 2025 — McMap. All rights reserved.