Created
August 23, 2020 17:33
-
-
Save corespider/1790bc5a5b0c5ec866823704840dace2 to your computer and use it in GitHub Desktop.
Worker Service in .NET Core-Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.Debug() | |
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) | |
.Enrich.FromLogContext() | |
.WriteTo.File(@"C:\LogFile.txt") | |
.CreateLogger(); | |
try | |
{ | |
Log.Information("Starting up the service"); | |
CreateHostBuilder(args).Build().Run(); | |
return; | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal(ex, "There was a problem starting the serivce"); | |
return; | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) | |
{ | |
return Host.CreateDefaultBuilder(args) | |
.UseWindowsService() | |
.ConfigureServices((hostContext, services) => | |
{ | |
services.AddHostedService<Worker>(); | |
}) | |
.UseSerilog(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment