|
1 | | -using System.Globalization; |
2 | 1 | using Api.Endpoints; |
3 | 2 | using Api.Setup; |
4 | | -using Azure.Identity; |
5 | | -using Common; |
6 | | -using Microsoft.ApplicationInsights.Extensibility; |
7 | 3 | using Serilog; |
8 | | -using Serilog.Exceptions; |
9 | | -using Serilog.Formatting; |
10 | | -using Serilog.Formatting.Compact; |
11 | | -using Serilog.Formatting.Display; |
12 | 4 |
|
13 | 5 | DotNetEnv.Env.TraversePath().Load(); |
14 | 6 |
|
15 | | -var config = new ConfigurationBuilder() |
16 | | - .SetBasePath(Directory.GetCurrentDirectory()) |
17 | | - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
18 | | - .AddEnvironmentVariables() |
19 | | - .Build(); |
20 | | - |
21 | | -const string logFormat = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message:lj}{NewLine}{Exception}"; |
22 | | - |
23 | | -Log.Logger = new LoggerConfiguration() |
24 | | - .ReadFrom.Configuration(config) |
25 | | - .WriteTo.Console(outputTemplate: logFormat, formatProvider: CultureInfo.InvariantCulture) |
26 | | - .CreateBootstrapLogger(); |
27 | | - |
28 | 7 | try |
29 | 8 | { |
| 9 | + Log.Logger = LoggingConfigurationExtensions.CreateBootstrapLogger(); |
| 10 | + |
30 | 11 | Log.Information("Starting up the application"); |
31 | 12 |
|
32 | 13 | var builder = WebApplication.CreateBuilder(args); |
33 | 14 |
|
34 | | - var keyVaultName = builder.Configuration.GetValue<string>(EnvVarKeys.KeyVaultName); |
35 | | - if (builder.Environment.IsProduction() && !string.IsNullOrEmpty(keyVaultName)) |
36 | | - { |
37 | | - var keyVaultUri = new Uri($"https://{keyVaultName}.vault.azure.net/"); |
38 | | - builder.Configuration.AddAzureKeyVault(keyVaultUri, new DefaultAzureCredential()); |
39 | | - } |
| 15 | + builder.Configuration.AddConfiguration(builder.Environment); |
40 | 16 |
|
41 | | - builder.Services.ConfigureServices(builder.Environment); |
| 17 | + builder.Services.AddServices(builder.Environment); |
42 | 18 |
|
43 | 19 | builder.Host.UseSerilog((ctx, services, loggerConfig) => |
44 | | - { |
45 | | - ITextFormatter formatter = new MessageTemplateTextFormatter(outputTemplate: logFormat, formatProvider: CultureInfo.InvariantCulture); |
46 | | - |
47 | | - if (EnvVarAccessors.UseJsonFormatting) |
48 | | - { |
49 | | - formatter = new CompactJsonFormatter(); |
50 | | - } |
51 | | - |
52 | | - loggerConfig |
53 | | - .ReadFrom.Configuration(ctx.Configuration) |
54 | | - .Enrich.FromLogContext() |
55 | | - .Enrich.WithExceptionDetails() |
56 | | - .WriteTo.Async(x => x.Console(formatter)); |
57 | | - |
58 | | - if (ctx.HostingEnvironment.IsProduction()) |
59 | | - { |
60 | | - var tc = new TelemetryConfiguration |
61 | | - { |
62 | | - ConnectionString = ctx.Configuration["ApplicationInsights:ConnectionString"] |
63 | | - }; |
64 | | - loggerConfig.WriteTo.ApplicationInsights(tc, TelemetryConverter.Traces); |
65 | | - } |
66 | | - }, |
| 20 | + loggerConfig.AddLoggingConfiguration(ctx.Configuration, builder.Environment), |
67 | 21 | writeToProviders: false, |
68 | 22 | preserveStaticLogger: false |
69 | 23 | ); |
70 | 24 |
|
71 | | - |
72 | 25 | var app = builder.Build(); |
73 | 26 |
|
74 | 27 | app.MapEndpoints(); |
75 | 28 |
|
76 | | - app.ConfigureMiddleware(app.Environment, app.Configuration); |
| 29 | + app.AddMiddleware(app.Environment, app.Configuration); |
77 | 30 |
|
78 | 31 | await app.RunAsync().ConfigureAwait(false); |
79 | 32 | } |
|
0 commit comments