|
1 | | -using Elastic.CommonSchema; |
| 1 | +using System.Globalization; |
| 2 | +using Elastic.CommonSchema; |
2 | 3 | using Elastic.CommonSchema.Serilog; |
3 | 4 | using Microsoft.AspNetCore.Builder; |
4 | 5 | using Microsoft.Extensions.DependencyInjection; |
@@ -104,18 +105,37 @@ private static LoggerConfiguration WriteToFile(this LoggerConfiguration loggerCo |
104 | 105 | { |
105 | 106 | var ecsConfig = new EcsTextFormatterConfiguration |
106 | 107 | { |
| 108 | + MessageFormatProvider = CultureInfo.InvariantCulture, |
107 | 109 | IncludeHost = false, |
108 | 110 | IncludeProcess = false, |
109 | 111 | IncludeUser = false, |
110 | 112 |
|
111 | | - // Last-chance mutator: remove agent.* |
112 | 113 | MapCustom = (doc, _) => |
113 | 114 | { |
114 | 115 | doc.Agent = null; |
| 116 | + |
| 117 | + if (doc.Labels is { Count: > 0 }) |
| 118 | + { |
| 119 | + doc.Labels.Remove("MessageTemplate"); |
| 120 | + } |
| 121 | + |
| 122 | + if (doc.Event != null) |
| 123 | + { |
| 124 | + var dur = doc.Event.Duration; |
| 125 | + doc.Event = new Event |
| 126 | + { |
| 127 | + Duration = dur |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + if (doc.Service != null) |
| 132 | + { |
| 133 | + doc.Service.Type = null; |
| 134 | + } |
| 135 | + |
115 | 136 | return doc; |
116 | 137 | } |
117 | 138 | }; |
118 | | - // Choose the formatter based on the selected log backend |
119 | 139 | ITextFormatter formatter = logBackend switch |
120 | 140 | { |
121 | 141 | LogBackend.ElasticSearch => new EcsTextFormatter(ecsConfig), |
@@ -150,29 +170,17 @@ private static LoggerConfiguration FilterOutUnwantedLogs(this LoggerConfiguratio |
150 | 170 |
|
151 | 171 | private static bool ShouldDropByPath(LogEvent evt) |
152 | 172 | { |
153 | | - // Try Url first (absolute), then RequestPath, then Path |
154 | | - var raw = GetScalar(evt, "Url") ?? GetScalar(evt, "RequestPath") ?? GetScalar(evt, "Path"); |
155 | | - |
156 | | - if (string.IsNullOrEmpty(raw)) |
| 173 | + if (!evt.Properties.TryGetValue("RequestPath", out var p) || p is not ScalarValue sv) |
157 | 174 | { |
158 | 175 | return false; |
159 | 176 | } |
160 | 177 |
|
161 | | - // If it's a full URL, extract the path |
162 | | - var path = raw; |
163 | | - if (Uri.TryCreate(raw, UriKind.Absolute, out var uri)) |
164 | | - { |
165 | | - path = uri.AbsolutePath; |
166 | | - } |
167 | | - |
168 | | - // Case-insensitive match on path fragments you want to drop |
169 | | - return path.Contains("/swagger", StringComparison.OrdinalIgnoreCase) |
170 | | - || path.Contains("/hangfire", StringComparison.OrdinalIgnoreCase) |
171 | | - || path.Contains("/above-board", StringComparison.OrdinalIgnoreCase) |
172 | | - || path.Contains("is-authenticated.json?api-version=v2", StringComparison.OrdinalIgnoreCase); |
| 178 | + var path = sv.Value as string ?? ""; |
173 | 179 |
|
174 | | - static string? GetScalar(LogEvent e, string name) => |
175 | | - e.Properties.TryGetValue(name, out var v) && v is ScalarValue sv && sv.Value is string s ? s : null; |
| 180 | + return path.StartsWith("/swagger") |
| 181 | + || path.StartsWith("/hangfire") |
| 182 | + || path.Contains("/above-board") |
| 183 | + || path.Contains("localhost/auth/is-authenticated.json?api-version=v2"); |
176 | 184 | } |
177 | 185 |
|
178 | 186 | private static bool IsEfOutboxQuery(LogEvent evt) |
|
0 commit comments