Skip to content

Commit dd50702

Browse files
authored
[Hosting] Add {OriginalFormat} to start & stop log state objects (#45253)
1 parent b6ec992 commit dd50702

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/Hosting/Hosting/src/Internal/HostingRequestFinishedLog.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ internal sealed class HostingRequestFinishedLog : IReadOnlyList<KeyValuePair<str
1313
{
1414
internal static readonly Func<object, Exception?, string> Callback = (state, exception) => ((HostingRequestFinishedLog)state).ToString();
1515

16+
private const string OriginalFormat = "Request finished {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} - {StatusCode} {ContentLength} {ContentType} {ElapsedMilliseconds}ms";
1617
private readonly HostingApplication.Context _context;
1718

1819
private string? _cachedToString;
1920
public TimeSpan Elapsed { get; }
2021

21-
public int Count => 11;
22+
public int Count => 12;
2223

2324
public KeyValuePair<string, object?> this[int index]
2425
{
@@ -42,6 +43,7 @@ internal sealed class HostingRequestFinishedLog : IReadOnlyList<KeyValuePair<str
4243
8 => new KeyValuePair<string, object?>(nameof(request.PathBase), request.PathBase.Value),
4344
9 => new KeyValuePair<string, object?>(nameof(request.Path), request.Path.Value),
4445
10 => new KeyValuePair<string, object?>(nameof(request.QueryString), request.QueryString.Value),
46+
11 => new KeyValuePair<string, object?>("{OriginalFormat}", OriginalFormat),
4547
_ => throw new IndexOutOfRangeException(nameof(index)),
4648
};
4749
}
@@ -57,10 +59,11 @@ public override string ToString()
5759
{
5860
if (_cachedToString == null)
5961
{
60-
Debug.Assert(_context.HttpContext != null && _context.StartLog != null);
62+
Debug.Assert(_context.HttpContext != null);
6163

64+
var request = _context.HttpContext.Request;
6265
var response = _context.HttpContext.Response;
63-
_cachedToString = $"Request finished {_context.StartLog.ToStringWithoutPreamble()} - {response.StatusCode.ToString(CultureInfo.InvariantCulture)} {ValueOrEmptyMarker(response.ContentLength)} {EscapedValueOrEmptyMarker(response.ContentType)} {Elapsed.TotalMilliseconds.ToString("0.0000", CultureInfo.InvariantCulture)}ms";
66+
_cachedToString = $"Request finished {request.Protocol} {request.Method} {request.Scheme}://{request.Host.Value}{request.PathBase.Value}{request.Path.Value}{request.QueryString.Value} - {response.StatusCode.ToString(CultureInfo.InvariantCulture)} {ValueOrEmptyMarker(response.ContentLength)} {EscapedValueOrEmptyMarker(response.ContentType)} {Elapsed.TotalMilliseconds.ToString("0.0000", CultureInfo.InvariantCulture)}ms";
6467
}
6568

6669
return _cachedToString;

src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Hosting;
99

1010
internal sealed class HostingRequestStartingLog : IReadOnlyList<KeyValuePair<string, object?>>
1111
{
12-
private const string LogPreamble = "Request starting ";
12+
private const string OriginalFormat = "Request starting {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} - {ContentType} {ContentLength}";
1313
private const string EmptyEntry = "-";
1414

1515
internal static readonly Func<object, Exception?, string> Callback = (state, exception) => ((HostingRequestStartingLog)state).ToString();
@@ -18,7 +18,7 @@ internal sealed class HostingRequestStartingLog : IReadOnlyList<KeyValuePair<str
1818

1919
private string? _cachedToString;
2020

21-
public int Count => 9;
21+
public int Count => 10;
2222

2323
public KeyValuePair<string, object?> this[int index] => index switch
2424
{
@@ -31,6 +31,7 @@ internal sealed class HostingRequestStartingLog : IReadOnlyList<KeyValuePair<str
3131
6 => new KeyValuePair<string, object?>(nameof(_request.PathBase), _request.PathBase.Value),
3232
7 => new KeyValuePair<string, object?>(nameof(_request.Path), _request.Path.Value),
3333
8 => new KeyValuePair<string, object?>(nameof(_request.QueryString), _request.QueryString.Value),
34+
9 => new KeyValuePair<string, object?>("{OriginalFormat}", OriginalFormat),
3435
_ => throw new IndexOutOfRangeException(nameof(index)),
3536
};
3637

@@ -44,7 +45,7 @@ public override string ToString()
4445
if (_cachedToString == null)
4546
{
4647
var request = _request;
47-
_cachedToString = $"{LogPreamble}{request.Protocol} {request.Method} {request.Scheme}://{request.Host.Value}{request.PathBase.Value}{request.Path.Value}{request.QueryString.Value} {EscapedValueOrEmptyMarker(request.ContentType)} {ValueOrEmptyMarker(request.ContentLength)}";
48+
_cachedToString = $"Request starting {request.Protocol} {request.Method} {request.Scheme}://{request.Host.Value}{request.PathBase.Value}{request.Path.Value}{request.QueryString.Value} - {EscapedValueOrEmptyMarker(request.ContentType)} {ValueOrEmptyMarker(request.ContentLength)}";
4849
}
4950

5051
return _cachedToString;
@@ -63,9 +64,6 @@ IEnumerator IEnumerable.GetEnumerator()
6364
return GetEnumerator();
6465
}
6566

66-
internal string ToStringWithoutPreamble()
67-
=> ToString().Substring(LogPreamble.Length);
68-
6967
internal static string EscapedValueOrEmptyMarker(string? potentialValue)
7068
// Encode space as +
7169
=> potentialValue?.Length > 0 ? potentialValue.Replace(' ', '+') : EmptyEntry;

src/Hosting/Hosting/test/Internal/HostingRequestStartLogTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace Microsoft.AspNetCore.Hosting.Tests;
99
public class HostingRequestStartLogTests
1010
{
1111
[Theory]
12-
[InlineData(",XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "Request starting GET 1.1 http://,XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//?query test 0")]
13-
[InlineData(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "Request starting GET 1.1 http:// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//?query test 0")]
12+
[InlineData(",XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "Request starting GET 1.1 http://,XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//?query - test 0")]
13+
[InlineData(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "Request starting GET 1.1 http:// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//?query - test 0")]
1414
public void InvalidHttpContext_DoesNotThrowOnAccessingProperties(string input, string expected)
1515
{
1616
var mockRequest = new Mock<HttpRequest>();

0 commit comments

Comments
 (0)