Quantcast
Channel: briancaos – Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 276

C# Set local folder for .net Core Windows Services

$
0
0

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:

 


Viewing all articles
Browse latest Browse all 276

Trending Articles