-
Notifications
You must be signed in to change notification settings - Fork 10
/
Program.cs
81 lines (74 loc) · 3.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace Todo.WebApi
{
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Core;
/// <summary>
/// Console application used for running Todo ASP.NET Core Web API.
/// </summary>
[SuppressMessage("ReSharper", "S1135", Justification = "The todo word represents an entity")]
[ExcludeFromCodeCoverage]
public static class Program
{
private static readonly Logger logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
/// <summary>
/// Runs Todo ASP.NET Core Web API.
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
try
{
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
logger.Fatal(exception, "Todo ASP.NET Core Web API failed to start");
throw;
}
finally
{
logger.Dispose();
}
}
private static IHostBuilder CreateHostBuilder(string[] args)
{
logger.Information("Configuring host builder needed to run Todo ASP.NET Core Web API ...");
IHostBuilder hostBuilder =
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) =>
{
configurationBuilder.Sources.Clear();
configurationBuilder.SetBasePath(hostBuilderContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostBuilderContext.HostingEnvironment.EnvironmentName}.json",
optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args);
})
.ConfigureWebHostDefaults(localHostBuilder =>
{
// Ensure that when an error occurs during startup, host will exit.
// See more about capturing startup errors here:
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-5.0#capture-startup-errors.
localHostBuilder.CaptureStartupErrors(false);
// Ensure the application captures detailed errors.
// See more about detailed errors here:
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-5.0#detailed-errors.
localHostBuilder.UseSetting(WebHostDefaults.DetailedErrorsKey, bool.TrueString);
// See more about the Startup class here:
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-5.0.
localHostBuilder.UseStartup<Startup>();
});
logger.Information("Host builder needed to run Todo ASP.NET Core Web API has been configured");
return hostBuilder;
}
}
}