|
3 | 3 | using Api.Setup; |
4 | 4 | using Azure.Identity; |
5 | 5 | using Common; |
| 6 | +using Microsoft.ApplicationInsights.Extensibility; |
6 | 7 | using Serilog; |
| 8 | +using Serilog.Exceptions; |
7 | 9 |
|
8 | 10 | DotNetEnv.Env.TraversePath().Load(); |
9 | 11 |
|
|
31 | 33 | { |
32 | 34 | var keyVaultUri = new Uri($"https://{keyVaultName}.vault.azure.net/"); |
33 | 35 | builder.Configuration.AddAzureKeyVault(keyVaultUri, new DefaultAzureCredential()); |
34 | | - |
35 | | - Log.Logger = new LoggerConfiguration() |
36 | | - .ReadFrom.Configuration(builder.Configuration) |
37 | | - .WriteTo.Console(outputTemplate: logFormat, formatProvider: CultureInfo.InvariantCulture) |
38 | | - .WriteTo.ApplicationInsights(builder.Configuration["ApplicationInsights:ConnectionString"], TelemetryConverter.Traces) |
39 | | - .CreateLogger(); |
40 | 36 | } |
41 | 37 |
|
42 | 38 | builder.Services.ConfigureServices(); |
43 | 39 |
|
| 40 | + builder.Host.UseSerilog((ctx, services, loggerConfig) => |
| 41 | + { |
| 42 | + loggerConfig |
| 43 | + .ReadFrom.Configuration(ctx.Configuration) |
| 44 | + .Enrich.FromLogContext() |
| 45 | + .Enrich.WithExceptionDetails() |
| 46 | + .WriteTo.Console(outputTemplate: logFormat, formatProvider: CultureInfo.InvariantCulture); |
| 47 | + |
| 48 | + if (ctx.HostingEnvironment.IsProduction()) |
| 49 | + { |
| 50 | + var tc = new TelemetryConfiguration |
| 51 | + { |
| 52 | + ConnectionString = ctx.Configuration["ApplicationInsights:ConnectionString"] |
| 53 | + }; |
| 54 | + loggerConfig.WriteTo.ApplicationInsights(tc, TelemetryConverter.Traces); |
| 55 | + } |
| 56 | + }, |
| 57 | + writeToProviders: false, |
| 58 | + preserveStaticLogger: false |
| 59 | + ); |
| 60 | + |
| 61 | + |
44 | 62 | var app = builder.Build(); |
45 | 63 |
|
46 | 64 | app.MapEndpoints(); |
|
0 commit comments