|
17 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
19 | 19 | // SOFTWARE. |
| 20 | +using CommonLoggingConfig; |
20 | 21 | using DuplicatiIngress; |
21 | 22 | using MassTransit; |
22 | 23 | using MassTransit.SqlTransport.PostgreSql; |
|
25 | 26 | using Microsoft.IdentityModel.Tokens; |
26 | 27 | using RobotsTxt; |
27 | 28 | using Serilog; |
28 | | -using Serilog.Core; |
29 | | -using Serilog.Events; |
30 | 29 | using SimpleSecurityFilter; |
31 | 30 |
|
32 | 31 | var builder = WebApplication.CreateBuilder(args); |
33 | 32 |
|
34 | | -var envConfig = builder.Configuration.GetRequiredSection("Environment").Get<EnvironmentConfig>()!; |
35 | | -builder.Services.AddSingleton(envConfig); |
36 | | - |
37 | | -// Prepare logging |
38 | | -var envLogLevel = builder.Configuration.GetValue<string>("Serilog:MinimumLevel:Default"); |
39 | | -var logLevelSwitch = new LoggingLevelSwitch( |
40 | | - !string.IsNullOrWhiteSpace(envLogLevel) |
41 | | - ? Enum.Parse<LogEventLevel>(envLogLevel) |
42 | | - : LogEventLevel.Information); |
43 | | - |
44 | | -var logConfiguration = new LoggerConfiguration() |
45 | | - .Enrich.FromLogContext() |
46 | | - .Enrich.WithClientIp() |
47 | | - .Enrich.WithCorrelationId(headerName: "X-Request-Id") |
48 | | - .Enrich.WithRequestHeader("X-Forwarded-For") |
49 | | - .Enrich.WithRequestHeader("User-Agent") |
50 | | - .MinimumLevel.ControlledBy(logLevelSwitch) |
51 | | - .WriteTo.Console(); |
52 | | - |
53 | | -builder.Host.UseSerilog(); |
54 | | - |
55 | 33 | // Support the untracked local environment variables file for development |
56 | 34 | if (builder.Environment.IsDevelopment()) |
57 | 35 | { |
|
66 | 44 |
|
67 | 45 | builder.Configuration.AddEnvironmentVariables(); |
68 | 46 |
|
| 47 | +var envConfig = builder.Configuration.GetRequiredSection("Environment").Get<EnvironmentConfig>()!; |
| 48 | +builder.Services.AddSingleton(envConfig); |
| 49 | + |
69 | 50 | var serilogConfig = builder.Configuration.GetSection("Serilog").Get<SerilogConfig>(); |
70 | | -if (!string.IsNullOrWhiteSpace(serilogConfig?.SourceToken)) |
71 | | -{ |
72 | | - if (string.IsNullOrWhiteSpace(serilogConfig.Endpoint)) |
73 | | - { |
74 | | - logConfiguration = logConfiguration.WriteTo.BetterStack( |
75 | | - sourceToken: serilogConfig.SourceToken |
76 | | - ); |
77 | | - } |
78 | | - else |
79 | | - { |
80 | | - logConfiguration = logConfiguration.WriteTo.BetterStack( |
81 | | - sourceToken: serilogConfig.SourceToken, |
82 | | - betterStackEndpoint: serilogConfig.Endpoint |
83 | | - ); |
84 | | - } |
85 | | -} |
| 51 | +var extras = new LoggingExtras() { IsProd = envConfig.IsProd, Hostname = envConfig.Hostname, MachineName = envConfig.MachineName }; |
| 52 | + |
| 53 | +builder.AddCommonLogging(serilogConfig, extras); |
86 | 54 |
|
87 | | -builder.Services.AddHttpContextAccessor(); |
88 | 55 |
|
89 | 56 | var securityconfig = builder.Configuration.GetSection("Security").Get<SimpleSecurityOptions>(); |
90 | 57 | builder.AddSimpleSecurityFilter(securityconfig, msg => Log.Warning(msg)); |
|
116 | 83 |
|
117 | 84 | builder.Services.AddSingleton(KVPSButter.KVPSLoader.CreateIKVPS(envConfig.Storage)); |
118 | 85 |
|
119 | | -Log.Logger = logConfiguration |
120 | | - .Enrich.WithProperty("Hostname", envConfig.Hostname ?? Environment.MachineName) |
121 | | - .Enrich.WithProperty("MachineName", envConfig.MachineName) |
122 | | - .Enrich.WithProperty("IsProd", envConfig.IsProd) |
123 | | - .CreateLogger(); |
124 | | - |
125 | 86 | var preconfiguredTokenConfig = await PreconfiguredTokens.LoadFromStorage(builder.Configuration.GetSection("PreconfiguredTokens").Get<TokenRuleOverrideConfig>()); |
126 | 87 |
|
127 | 88 | builder.Services |
|
156 | 117 |
|
157 | 118 |
|
158 | 119 | var app = builder.Build(); |
159 | | -app.UseSerilogRequestLogging(options => |
160 | | -{ |
161 | | - options.EnrichDiagnosticContext = (diagnosticContext, httpContext) => |
162 | | - { |
163 | | - var proto = httpContext.Request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? httpContext.Request.Scheme; |
164 | | - diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value); |
165 | | - diagnosticContext.Set("HttpRequestType", httpContext.Request.Method); |
166 | | - diagnosticContext.Set("HttpRequestUrl", $"{proto}://{httpContext.Request.Host}{httpContext.Request.Path}{httpContext.Request.QueryString}"); |
167 | | - diagnosticContext.Set("HttpRequestId", httpContext.TraceIdentifier); |
168 | | - }; |
169 | | -}); |
| 120 | +app.UseCommonLogging(); |
170 | 121 |
|
171 | 122 | app.UseSimpleSecurityFilter(securityconfig); |
172 | 123 |
|
|
0 commit comments