Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 906ed5f

Browse files
committed
Fixes #597: Hosting logging needs to use structured logging instead of format strings
1 parent 2d88b99 commit 906ed5f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs

+25-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Globalization;
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.Extensions.Logging;
1011

@@ -126,7 +127,11 @@ public override string ToString()
126127
{
127128
if (_cachedToString == null)
128129
{
129-
_cachedToString = $"RequestId:{_httpContext.TraceIdentifier} RequestPath:{_httpContext.Request.Path}";
130+
_cachedToString = string.Format(
131+
CultureInfo.InvariantCulture,
132+
"RequestId:{0} RequestPath:{1}",
133+
_httpContext.TraceIdentifier,
134+
_httpContext.Request.Path);
130135
}
131136

132137
return _cachedToString;
@@ -201,7 +206,18 @@ public override string ToString()
201206
{
202207
if (_cachedToString == null)
203208
{
204-
_cachedToString = $"Request starting {_request.Protocol} {_request.Method} {_request.Scheme}://{_request.Host}{_request.PathBase}{_request.Path}{_request.QueryString} {_request.ContentType} {_request.ContentLength}";
209+
_cachedToString = string.Format(
210+
CultureInfo.InvariantCulture,
211+
"Request starting {0} {1} {2}://{3}{4}{5}{6} {7} {8}",
212+
_request.Protocol,
213+
_request.Method,
214+
_request.Scheme,
215+
_request.Host,
216+
_request.PathBase,
217+
_request.Path,
218+
_request.QueryString,
219+
_request.ContentType,
220+
_request.ContentLength);
205221
}
206222

207223
return _cachedToString;
@@ -227,7 +243,7 @@ private class HostingRequestFinished : IReadOnlyList<KeyValuePair<string, object
227243

228244
private readonly HttpContext _httpContext;
229245
private readonly TimeSpan _elapsed;
230-
246+
231247
private string _cachedToString;
232248

233249
public int Count
@@ -266,7 +282,12 @@ public override string ToString()
266282
{
267283
if (_cachedToString == null)
268284
{
269-
_cachedToString = $"Request finished in {_elapsed.TotalMilliseconds}ms {_httpContext.Response.StatusCode} {_httpContext.Response.ContentType}";
285+
_cachedToString = string.Format(
286+
CultureInfo.InvariantCulture,
287+
"Request finished in {0}ms {1} {2}",
288+
_elapsed.TotalMilliseconds,
289+
_httpContext.Response.StatusCode,
290+
_httpContext.Response.ContentType);
270291
}
271292

272293
return _cachedToString;

0 commit comments

Comments
 (0)