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"
}
}
}
}