|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using System.IO; |
4 | | -using System.Linq; |
5 | | -using System.Threading.Tasks; |
6 | 3 | using Microsoft.AspNetCore; |
7 | 4 | using Microsoft.AspNetCore.Hosting; |
8 | 5 | using Microsoft.Extensions.Configuration; |
9 | | -using Microsoft.Extensions.Logging; |
10 | | - |
11 | 6 | using Serilog; |
12 | 7 |
|
13 | 8 | namespace SimpleWebAPISample |
14 | 9 | { |
15 | 10 | public class Program |
16 | 11 | { |
17 | | - public static void Main(string[] args) |
| 12 | + public static int Main(string[] args) |
18 | 13 | { |
| 14 | + var configuration = new ConfigurationBuilder() |
| 15 | + .SetBasePath(Directory.GetCurrentDirectory()) |
| 16 | + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
| 17 | + .Build(); |
| 18 | + |
19 | 19 | Log.Logger = new LoggerConfiguration() |
| 20 | + .ReadFrom.Configuration(configuration) |
20 | 21 | .Enrich.FromLogContext() |
21 | | - .WriteTo.LiterateConsole() |
| 22 | + .WriteTo.Console() |
22 | 23 | .CreateLogger(); |
23 | | - |
24 | | - Log.Information("Getting the motors running..."); |
25 | 24 |
|
26 | | - BuildWebHost(args).Run(); |
27 | | - } |
| 25 | + try |
| 26 | + { |
| 27 | + Log.Information("Getting the motors running..."); |
28 | 28 |
|
29 | | - public static IWebHost BuildWebHost(string[] args) => |
30 | | - WebHost.CreateDefaultBuilder(args) |
31 | | - .UseStartup<Startup>() |
32 | | - .ConfigureLogging(log => |
33 | | - { |
34 | | - log.SetMinimumLevel(LogLevel.Information); |
35 | | - log.AddSerilog(logger: Log.Logger, dispose: true); |
36 | | - }) |
37 | | - .Build(); |
| 29 | + var host = WebHost.CreateDefaultBuilder(args) |
| 30 | + .UseStartup<Startup>() |
| 31 | + .UseConfiguration(configuration) |
| 32 | + .UseSerilog() |
| 33 | + .Build(); |
| 34 | + |
| 35 | + host.Run(); |
| 36 | + |
| 37 | + return 0; |
| 38 | + } |
| 39 | + catch (Exception ex) |
| 40 | + { |
| 41 | + Log.Fatal(ex, "Unhandled exception"); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + finally |
| 45 | + { |
| 46 | + Log.CloseAndFlush(); |
| 47 | + } |
| 48 | + } |
38 | 49 | } |
39 | 50 | } |
0 commit comments