[Worker Service] After moving to Preview 6, logging doesn't work anymore in a Windows Service
See original GitHub issueDescribe the bug
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.
To Reproduce
Steps to reproduce the behavior:
- Using this version of ASP.NET Core: 3.0.100-preview6-012264
- Create a new Worker Service project in Visual Studio 2019
- Install the following NuGet package: Microsoft.Extensions.Hosting.WindowsServices, version 3.0.0-preview6.19304.6
- Add .UseWindowsService() in the CreateHostBuilder() method
- From the command line, publish the project with dotnet publish to a folder (like C:\WorkerService)
- Register it as a Windows Service by using the command sc create worker1 binPath=c:\WorkerService\WorkerService.exe
- Run the service with sc start worker1
Expected behavior
All the logging is displayed in the Application section of the Windows logs inside Event Viewer
Additional context
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]
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top Results From Across the Web
c# - .Net Core 6 Worker Service not writing to Logs when ...
I have created a Worker Service using C# / .Net Core 6 (Visual Studio 2022). It writes to a log file as expected...
Read more >.NET Core Workers as Windows Services - .NET Blog
In this walkthrough we will create a worker and run it as a Windows Service. Create a worker. Preview Note: In our preview...
Read more >Create Windows Service using BackgroundService - .NET
Start and stop the Windows Service. View event logs. Delete the Windows Service. Tip. All of the "Workers in .NET" example source code...
Read more >Troubleshoot ASP.NET Core on Azure App Service and IIS
Azure App Services Log stream Streaming logs have some latency and might not display immediately.
Read more >Worker background service Serilog problem - Microsoft Q&A
Hi, I have a worker background service that works as a Windows service on production. The problem is, it does not log errors...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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