When developing .NET Core Worker Services, you can allow the service to run as Windows Service:
public static IHostBuilder CreateHostBuilder(string[] args) { var host = Host.CreateDefaultBuilder(args); host.UseWindowsService(); ... ...
The side effect is that the root folder changes from the local folder to the System32 folder, which means that any log files that you would expect to find in your local folder suddenly ends up in another folder.
The fix is easy, simple add the following to the main function of your application:
public static void Main(string[] args) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); CreateHostBuilder(args).Build().Run(); }
SetCurrentDirectory will then rebase the local folder to the base directory of your application, and your log files will be written to the local folder.
MORE TO READ:
- What directory does a Windows Service run in? from stackoverflow
- Background tasks with hosted services in ASP.NET Core from Microsoft
- .NET Core Workers as Windows Services from Microsoft
- .NET Core Worker Services with Application Insights and Serilog by briancaos