I have a .NET Core application, writing to Windows event viewer.
I'd like to map some of the logs to one source and some to other (based on the caller class).
The setup in Program.cs
looks like that:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddEventLog(new EventLogSettings()
{
LogName = "CustomerSearchScheduler",
SourceName = "CustomerSearchScheduler",
Filter = (source, level) => source.Contains("Schedulers")
});
logging.AddEventLog(new EventLogSettings()
{
LogName = "CustomerSearch",
SourceName = "CustomerSearch",
Filter = (source, level) => source.Contains("CustomerSearch") && !source.Contains("Schedulers")
});
logging.AddConsole();
})
//Web Host defaults.........
}
Problem is, AddEventLog
s seem to override one another. With the code above, nothing is printed to CustomerSearch
and only CustomerSearchScheduler
shows new logs. When I remove the CustomerSearchScheduler
part, the second type works as expected.
How can I make the two work simultaneously?
Thanks
CodePudding user response:
You'll need to explicitly add the event log provider to the service collection:
logging.Services.AddSingleton<ILoggerProvider>(new EventLogLoggerProvider(settings));
The default will not add the service provider twice