Skip to content

Commit 0bc04d3

Browse files
committed
Stop creating the default logging scope in hosting
- We correlate logs with Activities and more information can be added to them (like the trace identifier) using activity listeners or custom middleware.
1 parent 0542b87 commit 0bc04d3

File tree

3 files changed

+0
-104
lines changed

3 files changed

+0
-104
lines changed

src/Hosting/Hosting/src/Internal/HostingApplication.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public void DisposeContext(Context context, Exception? exception)
121121
internal sealed class Context
122122
{
123123
public HttpContext? HttpContext { get; set; }
124-
public IDisposable? Scope { get; set; }
125124
public Activity? Activity
126125
{
127126
get => HttpActivityFeature?.Activity;
@@ -153,7 +152,6 @@ public void Reset()
153152
{
154153
// Not resetting HttpContext here as we pool it on the Context
155154

156-
Scope = null;
157155
Activity = null;
158156
StartLog = null;
159157

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Collections;
54
using System.Diagnostics;
65
using System.Diagnostics.CodeAnalysis;
7-
using System.Globalization;
86
using System.Runtime.CompilerServices;
97
using Microsoft.AspNetCore.Http;
108
using Microsoft.AspNetCore.Http.Features;
@@ -97,11 +95,6 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
9795
// To avoid allocation, return a null scope if the logger is not on at least to some degree.
9896
if (loggingEnabled)
9997
{
100-
// Scope may be relevant for a different level of logging, so we always create it
101-
// see: https://github.com/aspnet/Hosting/pull/944
102-
// Scope can be null if logging is not on.
103-
context.Scope = Log.RequestScope(_logger, httpContext);
104-
10598
if (_logger.IsEnabled(LogLevel.Information))
10699
{
107100
if (startTimestamp == 0)
@@ -215,9 +208,6 @@ public void RequestEnd(HttpContext httpContext, Exception? exception, HostingApp
215208
_eventSource.RequestFailed();
216209
}
217210
}
218-
219-
// Logging Scope is finshed with
220-
context.Scope?.Dispose();
221211
}
222212

223213
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -506,74 +496,4 @@ private void StopActivity(Activity activity, HttpContext httpContext)
506496
WriteDiagnosticEvent(_diagnosticListener, ActivityStopKey, httpContext);
507497
activity.Stop(); // Resets Activity.Current (we want this after the Write)
508498
}
509-
510-
private static class Log
511-
{
512-
public static IDisposable? RequestScope(ILogger logger, HttpContext httpContext)
513-
{
514-
return logger.BeginScope(new HostingLogScope(httpContext));
515-
}
516-
517-
private sealed class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
518-
{
519-
private readonly string _path;
520-
private readonly string _traceIdentifier;
521-
522-
private string? _cachedToString;
523-
524-
public int Count => 2;
525-
526-
public KeyValuePair<string, object> this[int index]
527-
{
528-
get
529-
{
530-
if (index == 0)
531-
{
532-
return new KeyValuePair<string, object>("RequestId", _traceIdentifier);
533-
}
534-
else if (index == 1)
535-
{
536-
return new KeyValuePair<string, object>("RequestPath", _path);
537-
}
538-
539-
throw new ArgumentOutOfRangeException(nameof(index));
540-
}
541-
}
542-
543-
public HostingLogScope(HttpContext httpContext)
544-
{
545-
_traceIdentifier = httpContext.TraceIdentifier;
546-
_path = (httpContext.Request.PathBase.HasValue
547-
? httpContext.Request.PathBase + httpContext.Request.Path
548-
: httpContext.Request.Path).ToString();
549-
}
550-
551-
public override string ToString()
552-
{
553-
if (_cachedToString == null)
554-
{
555-
_cachedToString = string.Format(
556-
CultureInfo.InvariantCulture,
557-
"RequestPath:{0} RequestId:{1}",
558-
_path,
559-
_traceIdentifier);
560-
}
561-
562-
return _cachedToString;
563-
}
564-
565-
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
566-
{
567-
for (var i = 0; i < Count; ++i)
568-
{
569-
yield return this[i];
570-
}
571-
}
572-
573-
IEnumerator IEnumerable.GetEnumerator()
574-
{
575-
return GetEnumerator();
576-
}
577-
}
578-
}
579499
}

src/Hosting/Hosting/test/WebHostTests.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -868,28 +868,6 @@ public async Task IsEnvironment_Extension_Is_Case_Insensitive()
868868
}
869869
}
870870

871-
[Fact]
872-
public async Task WebHost_CreatesDefaultRequestIdentifierFeature_IfNotPresent()
873-
{
874-
// Arrange
875-
var requestDelegate = new RequestDelegate(httpContext =>
876-
{
877-
// Assert
878-
Assert.NotNull(httpContext);
879-
var featuresTraceIdentifier = httpContext.Features.Get<IHttpRequestIdentifierFeature>().TraceIdentifier;
880-
Assert.False(string.IsNullOrWhiteSpace(httpContext.TraceIdentifier));
881-
Assert.Same(httpContext.TraceIdentifier, featuresTraceIdentifier);
882-
883-
return Task.CompletedTask;
884-
});
885-
886-
using (var host = CreateHost(requestDelegate))
887-
{
888-
// Act
889-
await host.StartAsync();
890-
}
891-
}
892-
893871
[Fact]
894872
public async Task WebHost_DoesNot_CreateDefaultRequestIdentifierFeature_IfPresent()
895873
{

0 commit comments

Comments
 (0)