Skip to content

Commit ac81287

Browse files
authored
Structured log in HttpLoggingMiddleware (#33086)
1 parent 62998ac commit ac81287

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private async Task InvokeInternal(HttpContext context)
7979
if ((HttpLoggingFields.Request & options.LoggingFields) != HttpLoggingFields.None)
8080
{
8181
var request = context.Request;
82-
var list = new List<KeyValuePair<string, string?>>(
82+
var list = new List<KeyValuePair<string, object?>>(
8383
request.Headers.Count + DefaultRequestFieldsMinusHeaders);
8484

8585
if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestProtocol))
@@ -202,20 +202,19 @@ private async Task InvokeInternal(HttpContext context)
202202
}
203203
}
204204

205-
private static void AddToList(List<KeyValuePair<string, string?>> list, string key, string? value)
205+
private static void AddToList(List<KeyValuePair<string, object?>> list, string key, string? value)
206206
{
207-
list.Add(new KeyValuePair<string, string?>(key, value));
207+
list.Add(new KeyValuePair<string, object?>(key, value));
208208
}
209209

210210
public static void LogResponseHeaders(HttpResponse response, HttpLoggingOptions options, ILogger logger)
211211
{
212-
var list = new List<KeyValuePair<string, string?>>(
212+
var list = new List<KeyValuePair<string, object?>>(
213213
response.Headers.Count + DefaultResponseFieldsMinusHeaders);
214214

215215
if (options.LoggingFields.HasFlag(HttpLoggingFields.ResponseStatusCode))
216216
{
217-
list.Add(new KeyValuePair<string, string?>(nameof(response.StatusCode),
218-
response.StatusCode.ToString(CultureInfo.InvariantCulture)));
217+
list.Add(new KeyValuePair<string, object?>(nameof(response.StatusCode), response.StatusCode));
219218
}
220219

221220
if (options.LoggingFields.HasFlag(HttpLoggingFields.ResponseHeaders))
@@ -228,7 +227,7 @@ public static void LogResponseHeaders(HttpResponse response, HttpLoggingOptions
228227
logger.ResponseLog(httpResponseLog);
229228
}
230229

231-
internal static void FilterHeaders(List<KeyValuePair<string, string?>> keyValues,
230+
internal static void FilterHeaders(List<KeyValuePair<string, object?>> keyValues,
232231
IHeaderDictionary headers,
233232
HashSet<string> allowedHeaders)
234233
{
@@ -237,10 +236,10 @@ internal static void FilterHeaders(List<KeyValuePair<string, string?>> keyValues
237236
if (!allowedHeaders.Contains(key))
238237
{
239238
// Key is not among the "only listed" headers.
240-
keyValues.Add(new KeyValuePair<string, string?>(key, Redacted));
239+
keyValues.Add(new KeyValuePair<string, object?>(key, Redacted));
241240
continue;
242241
}
243-
keyValues.Add(new KeyValuePair<string, string?>(key, value.ToString()));
242+
keyValues.Add(new KeyValuePair<string, object?>(key, value.ToString()));
244243
}
245244
}
246245
}

src/Middleware/HttpLogging/src/HttpRequestLog.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88

99
namespace Microsoft.AspNetCore.HttpLogging
1010
{
11-
internal sealed class HttpRequestLog : IReadOnlyList<KeyValuePair<string, string?>>
11+
internal sealed class HttpRequestLog : IReadOnlyList<KeyValuePair<string, object?>>
1212
{
13-
private readonly List<KeyValuePair<string, string?>> _keyValues;
13+
private readonly List<KeyValuePair<string, object?>> _keyValues;
1414
private string? _cachedToString;
1515

1616
internal static readonly Func<object, Exception?, string> Callback = (state, exception) => ((HttpRequestLog)state).ToString();
1717

18-
public HttpRequestLog(List<KeyValuePair<string, string?>> keyValues)
18+
public HttpRequestLog(List<KeyValuePair<string, object?>> keyValues)
1919
{
2020
_keyValues = keyValues;
2121
}
2222

23-
public KeyValuePair<string, string?> this[int index] => _keyValues[index];
23+
public KeyValuePair<string, object?> this[int index] => _keyValues[index];
2424

2525
public int Count => _keyValues.Count;
2626

27-
public IEnumerator<KeyValuePair<string, string?>> GetEnumerator()
27+
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
2828
{
2929
var count = _keyValues.Count;
3030
for (var i = 0; i < count; i++)

src/Middleware/HttpLogging/src/HttpResponseLog.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88

99
namespace Microsoft.AspNetCore.HttpLogging
1010
{
11-
internal sealed class HttpResponseLog : IReadOnlyList<KeyValuePair<string, string?>>
11+
internal sealed class HttpResponseLog : IReadOnlyList<KeyValuePair<string, object?>>
1212
{
13-
private readonly List<KeyValuePair<string, string?>> _keyValues;
13+
private readonly List<KeyValuePair<string, object?>> _keyValues;
1414
private string? _cachedToString;
1515

1616
internal static readonly Func<object, Exception?, string> Callback = (state, exception) => ((HttpResponseLog)state).ToString();
1717

18-
public HttpResponseLog(List<KeyValuePair<string, string?>> keyValues)
18+
public HttpResponseLog(List<KeyValuePair<string, object?>> keyValues)
1919
{
2020
_keyValues = keyValues;
2121
}
2222

23-
public KeyValuePair<string, string?> this[int index] => _keyValues[index];
23+
public KeyValuePair<string, object?> this[int index] => _keyValues[index];
2424

2525
public int Count => _keyValues.Count;
2626

27-
public IEnumerator<KeyValuePair<string, string?>> GetEnumerator()
27+
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
2828
{
2929
var count = _keyValues.Count;
3030
for (var i = 0; i < count; i++)

0 commit comments

Comments
 (0)