14

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?

3 Answers 3

18

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"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

6
+200

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.

Comments

2

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

1 Comment

hi Paul, LogError can write into Windows EventLog, but LogInformation not work, how to make it work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.