Skip to content

Structured log in HttpLoggingMiddleware #33086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private async Task InvokeInternal(HttpContext context)
if ((HttpLoggingFields.Request & options.LoggingFields) != HttpLoggingFields.None)
{
var request = context.Request;
var list = new List<KeyValuePair<string, string?>>(
var list = new List<KeyValuePair<string, object?>>(
request.Headers.Count + DefaultRequestFieldsMinusHeaders);

if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestProtocol))
Expand Down Expand Up @@ -202,20 +202,19 @@ private async Task InvokeInternal(HttpContext context)
}
}

private static void AddToList(List<KeyValuePair<string, string?>> list, string key, string? value)
private static void AddToList(List<KeyValuePair<string, object?>> list, string key, string? value)
{
list.Add(new KeyValuePair<string, string?>(key, value));
list.Add(new KeyValuePair<string, object?>(key, value));
}

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

if (options.LoggingFields.HasFlag(HttpLoggingFields.ResponseStatusCode))
{
list.Add(new KeyValuePair<string, string?>(nameof(response.StatusCode),
response.StatusCode.ToString(CultureInfo.InvariantCulture)));
list.Add(new KeyValuePair<string, object?>(nameof(response.StatusCode), response.StatusCode));
}

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

internal static void FilterHeaders(List<KeyValuePair<string, string?>> keyValues,
internal static void FilterHeaders(List<KeyValuePair<string, object?>> keyValues,
IHeaderDictionary headers,
HashSet<string> allowedHeaders)
{
Expand All @@ -237,10 +236,10 @@ internal static void FilterHeaders(List<KeyValuePair<string, string?>> keyValues
if (!allowedHeaders.Contains(key))
{
// Key is not among the "only listed" headers.
keyValues.Add(new KeyValuePair<string, string?>(key, Redacted));
keyValues.Add(new KeyValuePair<string, object?>(key, Redacted));
continue;
}
keyValues.Add(new KeyValuePair<string, string?>(key, value.ToString()));
keyValues.Add(new KeyValuePair<string, object?>(key, value.ToString()));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Middleware/HttpLogging/src/HttpRequestLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

namespace Microsoft.AspNetCore.HttpLogging
{
internal sealed class HttpRequestLog : IReadOnlyList<KeyValuePair<string, string?>>
internal sealed class HttpRequestLog : IReadOnlyList<KeyValuePair<string, object?>>
{
private readonly List<KeyValuePair<string, string?>> _keyValues;
private readonly List<KeyValuePair<string, object?>> _keyValues;
private string? _cachedToString;

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

public HttpRequestLog(List<KeyValuePair<string, string?>> keyValues)
public HttpRequestLog(List<KeyValuePair<string, object?>> keyValues)
{
_keyValues = keyValues;
}

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

public int Count => _keyValues.Count;

public IEnumerator<KeyValuePair<string, string?>> GetEnumerator()
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
var count = _keyValues.Count;
for (var i = 0; i < count; i++)
Expand Down
10 changes: 5 additions & 5 deletions src/Middleware/HttpLogging/src/HttpResponseLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

namespace Microsoft.AspNetCore.HttpLogging
{
internal sealed class HttpResponseLog : IReadOnlyList<KeyValuePair<string, string?>>
internal sealed class HttpResponseLog : IReadOnlyList<KeyValuePair<string, object?>>
{
private readonly List<KeyValuePair<string, string?>> _keyValues;
private readonly List<KeyValuePair<string, object?>> _keyValues;
private string? _cachedToString;

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

public HttpResponseLog(List<KeyValuePair<string, string?>> keyValues)
public HttpResponseLog(List<KeyValuePair<string, object?>> keyValues)
{
_keyValues = keyValues;
}

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

public int Count => _keyValues.Count;

public IEnumerator<KeyValuePair<string, string?>> GetEnumerator()
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
var count = _keyValues.Count;
for (var i = 0; i < count; i++)
Expand Down