| | 1 | | using System.Diagnostics; |
| | 2 | | using Microsoft.AspNetCore.Http.Features; |
| | 3 | | using Microsoft.Extensions.Options; |
| | 4 | | using OpenTelemetry.Exporter; |
| | 5 | | using Serilog; |
| | 6 | | using Serilog.Enrichers.Sensitive; |
| | 7 | | using Serilog.Enrichers.Span; |
| | 8 | | using Serilog.Events; |
| | 9 | | using Serilog.Exceptions; |
| | 10 | | using Serilog.Sinks.OpenTelemetry; |
| | 11 | | using static Gateway.OpenTelemetryIntegration; |
| | 12 | | using MicrosoftLogger = Microsoft.Extensions.Logging.ILogger; |
| | 13 | |
|
| | 14 | | namespace Gateway; |
| | 15 | |
|
| | 16 | | public static class Logging |
| | 17 | | { |
| 0 | 18 | | private static readonly Action< |
| 0 | 19 | | MicrosoftLogger, |
| 0 | 20 | | string, |
| 0 | 21 | | string, |
| 0 | 22 | | int?, |
| 0 | 23 | | TimeSpan, |
| 0 | 24 | | Exception? |
| 0 | 25 | | > _logCompletion = LoggerMessage.Define<string, string, int?, TimeSpan>( |
| 0 | 26 | | LogLevel.Information, |
| 0 | 27 | | new EventId(1, nameof(Logging)), |
| 0 | 28 | | "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed} ms" |
| 0 | 29 | | ); |
| | 30 | |
|
| | 31 | | public static WebApplication UseRequestLogging(this WebApplication app) |
| | 32 | | { |
| 0 | 33 | | app.Use( |
| 0 | 34 | | async (context, next) => |
| 0 | 35 | | { |
| 0 | 36 | | var stopWatch = Stopwatch.StartNew(); |
| 0 | 37 | | try |
| 0 | 38 | | { |
| 0 | 39 | | await next.Invoke(); |
| 0 | 40 | | stopWatch.Stop(); |
| 0 | 41 | | LogCompletion(context, stopWatch.Elapsed); |
| 0 | 42 | | } |
| 0 | 43 | | catch (Exception e) |
| 0 | 44 | | { |
| 0 | 45 | | if (stopWatch.IsRunning) |
| 0 | 46 | | stopWatch.Stop(); |
| 0 | 47 | | LogCompletion(context, stopWatch.Elapsed, e); |
| 0 | 48 | | throw; |
| 0 | 49 | | } |
| 0 | 50 | | } |
| 0 | 51 | | ); |
| 0 | 52 | | return app; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public static WebApplicationBuilder AddLogger(this WebApplicationBuilder builder) |
| | 56 | | { |
| 0 | 57 | | builder.Logging.ClearProviders(); |
| 0 | 58 | | builder.Host.UseSerilog( |
| 0 | 59 | | (_, sp, configuration) => |
| 0 | 60 | | { |
| 0 | 61 | | EnrichLog(configuration, sp); |
| 0 | 62 | | SinkLog(configuration, sp); |
| 0 | 63 | | } |
| 0 | 64 | | ); |
| | 65 | |
|
| 0 | 66 | | return builder; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | private static void SinkLog( |
| | 70 | | LoggerConfiguration logConfiguration, |
| | 71 | | IServiceProvider serviceProvider |
| | 72 | | ) |
| | 73 | | { |
| 0 | 74 | | var serviceMetaData = serviceProvider.GetRequiredService<IOptions<ServiceMetadata>>(); |
| 0 | 75 | | var openTelemetryOptions = serviceProvider.GetRequiredService< |
| 0 | 76 | | IOptions<OpenTelemetryOptions> |
| 0 | 77 | | >(); |
| | 78 | |
|
| | 79 | | const string LogTemplate = |
| | 80 | | "[{Timestamp:HH:mm:ss}][{Level:u3}][{SourceContext:1}] {Message:lj}{NewLine}{Exception}"; |
| 0 | 81 | | logConfiguration.WriteTo.Console(outputTemplate: LogTemplate); |
| 0 | 82 | | if (openTelemetryOptions.Value.Enabled) |
| | 83 | | { |
| 0 | 84 | | logConfiguration.WriteTo.OpenTelemetry(cfg => |
| 0 | 85 | | { |
| 0 | 86 | | cfg.Endpoint = $"{openTelemetryOptions.Value.Endpoint}/v1/logs"; |
| 0 | 87 | | cfg.Headers = openTelemetryOptions.Value.ParsedHeaders; |
| 0 | 88 | | cfg.Protocol = MapProtocol(openTelemetryOptions.Value.Protocol); |
| 0 | 89 | | cfg.IncludedData = |
| 0 | 90 | | IncludedData.MessageTemplateTextAttribute |
| 0 | 91 | | | IncludedData.TraceIdField |
| 0 | 92 | | | IncludedData.SpanIdField; |
| 0 | 93 | | cfg.ResourceAttributes = new Dictionary<string, object> |
| 0 | 94 | | { |
| 0 | 95 | | // TODO: Temporary solution, remove _logs |
| 0 | 96 | | // see https://github.com/dotnet/aspire/issues/1072 |
| 0 | 97 | | { "service.name", serviceMetaData.Value.ServiceName + "_logs" }, |
| 0 | 98 | | { "index", 10 }, |
| 0 | 99 | | { "flag", true }, |
| 0 | 100 | | { "value", 3.14 } |
| 0 | 101 | | }; |
| 0 | 102 | | }); |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | private static OtlpProtocol MapProtocol(OtlpExportProtocol protocol) |
| | 107 | | { |
| 0 | 108 | | return protocol switch |
| 0 | 109 | | { |
| 0 | 110 | | OtlpExportProtocol.Grpc => OtlpProtocol.Grpc, |
| 0 | 111 | | OtlpExportProtocol.HttpProtobuf => OtlpProtocol.HttpProtobuf, |
| 0 | 112 | | _ => OtlpProtocol.Grpc, |
| 0 | 113 | | }; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | private static void EnrichLog( |
| | 117 | | LoggerConfiguration configuration, |
| | 118 | | IServiceProvider serviceProvider |
| | 119 | | ) |
| | 120 | | { |
| 0 | 121 | | var serviceMetaData = serviceProvider.GetRequiredService<IOptions<ServiceMetadata>>(); |
| | 122 | |
|
| 0 | 123 | | configuration |
| 0 | 124 | | .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) |
| 0 | 125 | | .Enrich.WithProperty( |
| 0 | 126 | | nameof(serviceMetaData.Value.ServiceName), |
| 0 | 127 | | serviceMetaData.Value.ServiceName |
| 0 | 128 | | ) |
| 0 | 129 | | .Enrich.WithExceptionDetails() |
| 0 | 130 | | .Enrich.FromLogContext() |
| 0 | 131 | | .Enrich.WithThreadId() |
| 0 | 132 | | .Enrich.WithThreadName() |
| 0 | 133 | | .Enrich.WithProcessName() |
| 0 | 134 | | .Enrich.WithProcessId() |
| 0 | 135 | | .Enrich.WithEnvironmentName() |
| 0 | 136 | | .Enrich.WithMachineName() |
| 0 | 137 | | .Enrich.FromGlobalLogContext() |
| 0 | 138 | | .Enrich.WithClientIp() |
| 0 | 139 | | .Enrich.WithSensitiveDataMasking( |
| 0 | 140 | | (options) => |
| 0 | 141 | | { |
| 0 | 142 | | options.MaskingOperators = |
| 0 | 143 | | [ |
| 0 | 144 | | new EmailAddressMaskingOperator(), |
| 0 | 145 | | new IbanMaskingOperator(), |
| 0 | 146 | | new CreditCardMaskingOperator() |
| 0 | 147 | | ]; |
| 0 | 148 | | } |
| 0 | 149 | | ) |
| 0 | 150 | | .Enrich.WithSpan( |
| 0 | 151 | | new SpanOptions |
| 0 | 152 | | { |
| 0 | 153 | | IncludeBaggage = true, |
| 0 | 154 | | IncludeOperationName = true, |
| 0 | 155 | | IncludeTags = true |
| 0 | 156 | | } |
| 0 | 157 | | ); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | private static string GetPath(HttpContext httpContext) |
| | 161 | | { |
| 0 | 162 | | var requestPath = httpContext.Features.Get<IHttpRequestFeature>()?.Path; |
| 0 | 163 | | if (string.IsNullOrEmpty(requestPath)) |
| | 164 | | { |
| 0 | 165 | | requestPath = httpContext.Request.Path.ToString(); |
| | 166 | | } |
| | 167 | |
|
| 0 | 168 | | return requestPath!; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | private static void LogCompletion( |
| | 172 | | HttpContext httpContext, |
| | 173 | | TimeSpan elapsed, |
| | 174 | | Exception? exception = null |
| | 175 | | ) |
| | 176 | | { |
| 0 | 177 | | var logger = httpContext.RequestServices.GetRequiredService<ILogger<ApiLogging>>(); |
| 0 | 178 | | var method = httpContext.Request.Method; |
| 0 | 179 | | var path = GetPath(httpContext); |
| 0 | 180 | | var statusCode = httpContext.Response?.StatusCode; |
| 0 | 181 | | _logCompletion(logger, method, path, statusCode, elapsed, exception); |
| 0 | 182 | | } |
| | 183 | |
|
| 0 | 184 | | private record ApiLogging(); |
| | 185 | | } |