After moving to .NET Core 3.0 Preview 6, my Worker Service which is installed as a Windows Service doesn't output anymore logs in Event Viewer.
Steps to reproduce the behavior:
All the logging is displayed in the Application section of the Windows logs inside Event Viewer
With .NET Core 3.0 Preview 5, I was able to see the logging in Event Viewer just fine.
.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview6-012264
Commit: be3f0c1a03
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\
Host (useful for support):
Version: 3.0.0-preview6-27804-01
Commit: fdf81c6faf
.NET Core SDKs installed:
2.1.600 [C:\Program Files\dotnet\sdk]
2.1.601 [C:\Program Files\dotnet\sdk]
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.604 [C:\Program Files\dotnet\sdk]
2.1.700-preview-009618 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.800-preview-009677 [C:\Program Files\dotnet\sdk]
2.1.800-preview-009696 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
3.0.100-preview6-012264 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview6.19307.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
This is an unintentional side effect of fact that we add the event log provider by default now at the warning level.
https://github.com/aspnet/Extensions/blob/f12d709976e382672ce100bc00381a8847f06489/src/Hosting/Hosting/src/Host.cs#L102
You can work around this by adding this entry to your configuration:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}
This turns the level for the event log provider to Information for those categories.
We may need to re-think this or possibly remove the filter when running in a windows service.
cc @Tratcher
Removing the filter would mean we'd log Information by default to the EventLog right? That's likely to be pretty noisy.
Removing the filter would mean we'd log Information by default to the EventLog right? That's likely to be pretty noisy.
Right only for the windows service case I mean where everything is logged to the event log.
The change in filter level is by-design (logging only Warning and up by default to the Event Log), and the workaround @davidfowl posted should work. Closing as we don't plan to change this in 3.0.
I have a question is there a way to move the logs from Application section of the Windows logs inside Event Viewer and create a custom LogName as it possible if application is not calling .UseWindowsService()
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
//.ConfigureLogging(loggerFactory => loggerFactory.AddEventLog( new EventLogSettings() { SourceName = "CallerTestCacheServiceCoreAPI" , LogName = "CallerTestCacheServiceCoreAPILog" }))
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddEventLog(new EventLogSettings()
{
SourceName = "CallerTestCacheServiceCoreAPI",
LogName = "CallerTestCacheServiceCoreAPILog",
});
})
.ConfigureServices(services =>
{
services.AddTransient<CallCache>();
services.AddTransient<CallCacheEverySecond>();
services.AddHttpClient();
services.AddHostedService<WorkerEverySecond>();
services.AddHostedService<WorkerEveryMinute>();
});
No matter what im doing it i cannot overwrite the default logName in my example WorkerService1(my sample application has this name) Adding EventLogSettings() SourceName LogName just do not work
Don't call AddEventLog again, Configure the options in DI rather than passing them in directly:
https://github.com/aspnet/Extensions/blob/46d76f9a189ee68852d136c577364b7b0955257a/src/Hosting/WindowsServices/src/WindowsServiceLifetimeHostBuilderExtensions.cs#L41-L47
@Tratcher it`s work thank you very much
@nikolay-dimitrov i don't undestand the solution of @Tratcher . Please explain more better the solution? Thanks
@nikolay-dimitrov i don't undestand the solution of @Tratcher . Please explain more better the solution? Thanks
The solution is to add something like this:
services.Configure<EventLogSettings>(options =>
{
options.SourceName = "MySourceName";
options.LogName = "MyLogName";
});
in ConfigureServices() after you call .UseWindowsService().
Most helpful comment
This is an unintentional side effect of fact that we add the event log provider by default now at the warning level.
https://github.com/aspnet/Extensions/blob/f12d709976e382672ce100bc00381a8847f06489/src/Hosting/Hosting/src/Host.cs#L102
You can work around this by adding this entry to your configuration:
This turns the level for the event log provider to Information for those categories.
We may need to re-think this or possibly remove the filter when running in a windows service.
cc @Tratcher