0

I've been fighting to get logs working in newer version of .Net. If I don't set the SourceName and LogName, the log will show up in the windows event viewer as ".Net runtime". If I define them, it doesn't show anything. I've been searching but couldn't find a solution. Any help is appreciated. Thank you

string appName = "MyNewApp";
var loggerFactory = LoggerFactory.Create(
    builder => builder
                .ClearProviders()
                // add console as logging target
                .AddConsole()
                .AddEventLog(eventLogSettings =>
                {
                   eventLogSettings.SourceName = appName;
                   eventLogSettings.LogName = appName;
                })
                .SetMinimumLevel(LogLevel.Debug)
);
ILogger<Service> logger = loggerFactory.CreateLogger<Service>();
logger.LogCritical("Hello world");

The appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  }
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Nuno
  • 133
  • 7
  • [this example](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0#create-logs-in-main) resolves a logger using `GetRequiredService` rather than using the logger factory directly; it's possible the method you're using is not wired up correctly at this point in the application, does this make a difference? – Tom W Jul 04 '23 at 18:44
  • You also don't mention [creating the event source](https://stackoverflow.com/questions/54244285/how-do-i-write-to-a-custom-event-source-using-net-cores-iloggerfactory-is-the#54261626) which I believe is required, have you done this elsewhere? – Tom W Jul 04 '23 at 19:13
  • HI @TomW, that was it. I missed the event source creation. Thank you so much for your help. Best regards – Nuno Jul 05 '23 at 19:08

0 Answers0