|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Threading.Tasks; |
5 | 6 | using Microsoft.AspNetCore.Hosting; |
6 | 7 | using Microsoft.Extensions.Configuration; |
7 | 8 | using Microsoft.Extensions.Hosting; |
8 | 9 | using Microsoft.Extensions.Logging; |
| 10 | +using Serilog; |
| 11 | +using Serilog.Events; |
9 | 12 |
|
10 | 13 | namespace FacwareBase.API |
11 | 14 | { |
12 | 15 | public class Program |
13 | 16 | { |
| 17 | + /// <summary> |
| 18 | + /// App settings |
| 19 | + /// </summary> |
| 20 | + public static IConfiguration configuration = new ConfigurationBuilder() |
| 21 | + .SetBasePath(Directory.GetCurrentDirectory()) |
| 22 | + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
| 23 | + .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) |
| 24 | + .AddEnvironmentVariables() |
| 25 | + .Build(); |
| 26 | + |
14 | 27 | public static void Main(string[] args) |
15 | 28 | { |
16 | | - CreateHostBuilder(args).Build().Run(); |
| 29 | + Log.Logger = new LoggerConfiguration() |
| 30 | + .Enrich.FromLogContext() |
| 31 | + .ReadFrom.Configuration(configuration) |
| 32 | + .WriteTo.Debug() |
| 33 | + .WriteTo.ColoredConsole( |
| 34 | + LogEventLevel.Verbose, |
| 35 | + "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}") |
| 36 | + // TODO: Write to db or AWS cloud watch |
| 37 | + .CreateLogger(); |
| 38 | + |
| 39 | + try |
| 40 | + { |
| 41 | + Log.Information("Staring up"); |
| 42 | + CreateHostBuilder(args).Build().Run(); |
| 43 | + } |
| 44 | + catch (System.Exception exception) |
| 45 | + { |
| 46 | + Log.Fatal(exception, "Application fatils to start"); |
| 47 | + } |
| 48 | + finally |
| 49 | + { |
| 50 | + Log.CloseAndFlush(); |
| 51 | + } |
17 | 52 | } |
18 | 53 |
|
19 | 54 | public static IHostBuilder CreateHostBuilder(string[] args) => |
20 | 55 | Host.CreateDefaultBuilder(args) |
21 | | - .ConfigureWebHostDefaults(webBuilder => |
22 | | - { |
23 | | - webBuilder.UseStartup<Startup>(); |
24 | | - }); |
| 56 | + .UseSerilog() |
| 57 | + .ConfigureWebHostDefaults(webBuilder => |
| 58 | + { |
| 59 | + webBuilder.UseStartup<Startup>(); |
| 60 | + }); |
25 | 61 | } |
26 | 62 | } |
0 commit comments