David Tsui
David Tsui

Reputation: 533

How to write logs to EventLog by ILogger<T> in ASP.NET Core?

I follow this document Logging in .NET Core and ASP.NET Core, try to write log to Windows EventLog.

first, I create Source and Log in Windows Event Log:

if (!EventLog.SourceExists("MyTestSource"))
{
    EventLog.CreateEventSource("MyTestSource", "MyTestLog");
    return;
}

and it's created.

then, I configured logging in CreateHostBuilder from Program.cs of my ASP.NET Core app (core 3.0):

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddEventLog(new EventLogSettings
            {
                SourceName = "MyTestSource",
                LogName = "MyTestLog"
            });
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

I think that's all. Then I use logger in my controller:

[Route("[controller]")]
[ApiController]
public class ServerController : ControllerBase
{
    ILogger<ServerController> _logger = null;
    public ServerController(ILogger<ServerController> logger)
    {
        _logger = logger;
    }

    [HttpGet("GetServerInfo")]
    public string GetServerInfo()
    {
        _logger.LogInformation("GetServerInfo Called");
        return "Hello I'm Server";
    }
}

but there's nothing in MyTestLog in Windows EventLog. Is there anything I missed?

Upvotes: 14

Views: 9869

Answers (3)

pfx
pfx

Reputation: 23369

You can go for a configuration only approach, leaving your existing CreateHostBuilder code as-is.

As mentioned in the document you linked, under the Windows Eventlog section, the EventLogProvider defaults to the warning level when not explicitly configured:

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 lower this logging level to Information, you have to foresee an explicit entry for the EventLog provider in the Logging section of your appsettings.{Environment}.json file.

E.g.: appsettings.Development.json:

{
    "Logging": {
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        },
        "EventLog": {
            "LogLevel": {
                "Default": "Information"
            }
        }
    }
}

Upvotes: 16

Gio Tabidze
Gio Tabidze

Reputation: 361

It's caused by Host.CreateDefaultBuilder(args), which executes:

.ConfigureLogging(loggingBuilder => {
    loggingBuilder.AddFilter<EventLogLoggerProvider>((Func<LogLevel, bool>) (level=>level>=LogLevel.Warning));
}

under the hood. Warning is 1 level higher than Information, so...

As a solution, you can create fresh HostBuilder using new HostBuilder() and configure it from scratch or override that behavior by calling .ConfigureLogging after creating default host builder.

Upvotes: 6

Paul Lorica
Paul Lorica

Reputation: 763

Try bumping up your logging to _logger.LogError("GetServerInfo Called"") and see if it works. If it does, then you will have to set up your log filtering

Upvotes: 2

Related Questions