|
5 | 5 | using Microsoft.Extensions.Configuration; |
6 | 6 | using Serilog; |
7 | 7 | using Microsoft.Extensions.Hosting; |
| 8 | +using System.IO; |
| 9 | +using System; |
8 | 10 |
|
9 | 11 | namespace ReferenceProject |
10 | 12 | { |
11 | 13 | public class Program |
12 | 14 | { |
13 | | - public static void Main(string[] args) |
| 15 | + public static int Main(string[] args) |
14 | 16 | { |
15 | | - CreateHostBuilder(args).Build().Run(); |
| 17 | + try |
| 18 | + { |
| 19 | + CreateHostBuilder(args).Build().Run(); |
| 20 | + |
| 21 | + return 0; |
| 22 | + } |
| 23 | + catch(Exception ex) |
| 24 | + { |
| 25 | + var msg = "An unhandled exception occurred. The application will be closed"; |
| 26 | + Log.Logger?.Fatal(ex, msg); |
| 27 | + if( Log.Logger == null ) |
| 28 | + { |
| 29 | + Console.WriteLine(msg + Environment.NewLine + ex); |
| 30 | + } |
| 31 | + return 1; |
| 32 | + } |
16 | 33 | } |
17 | 34 |
|
18 | 35 | public static IHostBuilder CreateHostBuilder(string[] args) => |
19 | 36 | Host.CreateDefaultBuilder(args) |
20 | 37 | .UseServiceProviderFactory(new AutofacServiceProviderFactory()) |
| 38 | + .ConfigureAppConfiguration((hostingContext, config) => |
| 39 | + { |
| 40 | + // https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#using-environment-variables-in-configuration-options |
| 41 | + var envPath = Path.Combine(hostingContext.HostingEnvironment.ContentRootPath, ".env"); |
| 42 | + DotNetEnv.Env.Load(envPath); |
| 43 | + |
| 44 | + config.AddEnvironmentVariables(); |
| 45 | + }) |
21 | 46 | .ConfigureLogging((context, logging) => |
22 | 47 | { |
23 | 48 | logging.ClearProviders(); |
24 | 49 |
|
25 | | - /* |
26 | | - * You can use a global logger as this, but I don't recommend this way |
27 | | - * More information: https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#logging |
| 50 | + // https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#logging |
28 | 51 | Log.Logger = new LoggerConfiguration() |
29 | 52 | .ReadFrom.Configuration(context.Configuration) |
30 | 53 | .CreateLogger(); |
31 | | - */ |
32 | 54 |
|
33 | | - logging.AddSerilog(new LoggerConfiguration() |
34 | | - .ReadFrom.Configuration(context.Configuration) |
35 | | - .CreateLogger()); |
36 | | - }) |
37 | | - .ConfigureAppConfiguration(x => |
38 | | - { |
39 | | - x.AddEnvironmentVariables(); |
| 55 | + logging.AddSerilog(Log.Logger); |
40 | 56 | }) |
41 | 57 | .ConfigureWebHostDefaults(webBuilder => |
42 | 58 | { |
|
0 commit comments