question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Worker Service] After moving to Preview 6, logging doesn't work anymore in a Windows Service

See original GitHub issue

Describe 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:

  1. Using this version of ASP.NET Core: 3.0.100-preview6-012264
  2. Create a new Worker Service project in Visual Studio 2019
  3. Install the following NuGet package: Microsoft.Extensions.Hosting.WindowsServices, version 3.0.0-preview6.19304.6
  4. Add .UseWindowsService() in the CreateHostBuilder() method
  5. From the command line, publish the project with dotnet publish to a folder (like C:\WorkerService)
  6. Register it as a Windows Service by using the command sc create worker1 binPath=c:\WorkerService\WorkerService.exe
  7. 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
davidfowlcommented, Jun 13, 2019

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

1reaction
Tratchercommented, Sep 23, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found