Skip to content

Commit 57b4781

Browse files
committed
Remove default logging scope from ASP.NET Core
- We log the activity information by default, the span id is more useful than the traceidentifier. - Add the path and trace id as activity tags
1 parent d417c3a commit 57b4781

File tree

2 files changed

+5
-80
lines changed

2 files changed

+5
-80
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public void DisposeContext(Context context, Exception? exception)
119119
internal sealed class Context
120120
{
121121
public HttpContext? HttpContext { get; set; }
122-
public IDisposable? Scope { get; set; }
123122
public Activity? Activity
124123
{
125124
get => HttpActivityFeature?.Activity;
@@ -147,7 +146,6 @@ public void Reset()
147146
{
148147
// Not resetting HttpContext here as we pool it on the Context
149148

150-
Scope = null;
151149
Activity = null;
152150
StartLog = null;
153151

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

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
6565

6666
if (context.Activity is Activity activity)
6767
{
68+
// Add some ASP.NET Core specific properties as tags
69+
activity.AddTag("aspnet.traceidentifier", httpContext.TraceIdentifier);
70+
var path = (httpContext.Request.PathBase.HasValue ? httpContext.Request.PathBase + httpContext.Request.Path : httpContext.Request.Path).ToString();
71+
activity.AddTag("aspnet.requestpath", path);
72+
6873
if (httpContext.Features.Get<IHttpActivityFeature>() is IHttpActivityFeature feature)
6974
{
7075
feature.Activity = activity;
@@ -88,11 +93,6 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
8893
// To avoid allocation, return a null scope if the logger is not on at least to some degree.
8994
if (loggingEnabled)
9095
{
91-
// Scope may be relevant for a different level of logging, so we always create it
92-
// see: https://github.com/aspnet/Hosting/pull/944
93-
// Scope can be null if logging is not on.
94-
context.Scope = Log.RequestScope(_logger, httpContext);
95-
9696
if (_logger.IsEnabled(LogLevel.Information))
9797
{
9898
if (startTimestamp == 0)
@@ -173,9 +173,6 @@ public void RequestEnd(HttpContext httpContext, Exception? exception, HostingApp
173173
HostingEventSource.Log.RequestFailed();
174174
}
175175
}
176-
177-
// Logging Scope is finshed with
178-
context.Scope?.Dispose();
179176
}
180177

181178
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -453,74 +450,4 @@ private void StopActivity(Activity activity, HttpContext httpContext)
453450
WriteDiagnosticEvent(_diagnosticListener, ActivityStopKey, httpContext);
454451
activity.Stop(); // Resets Activity.Current (we want this after the Write)
455452
}
456-
457-
private static class Log
458-
{
459-
public static IDisposable? RequestScope(ILogger logger, HttpContext httpContext)
460-
{
461-
return logger.BeginScope(new HostingLogScope(httpContext));
462-
}
463-
464-
private sealed class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
465-
{
466-
private readonly string _path;
467-
private readonly string _traceIdentifier;
468-
469-
private string? _cachedToString;
470-
471-
public int Count => 2;
472-
473-
public KeyValuePair<string, object> this[int index]
474-
{
475-
get
476-
{
477-
if (index == 0)
478-
{
479-
return new KeyValuePair<string, object>("RequestId", _traceIdentifier);
480-
}
481-
else if (index == 1)
482-
{
483-
return new KeyValuePair<string, object>("RequestPath", _path);
484-
}
485-
486-
throw new ArgumentOutOfRangeException(nameof(index));
487-
}
488-
}
489-
490-
public HostingLogScope(HttpContext httpContext)
491-
{
492-
_traceIdentifier = httpContext.TraceIdentifier;
493-
_path = (httpContext.Request.PathBase.HasValue
494-
? httpContext.Request.PathBase + httpContext.Request.Path
495-
: httpContext.Request.Path).ToString();
496-
}
497-
498-
public override string ToString()
499-
{
500-
if (_cachedToString == null)
501-
{
502-
_cachedToString = string.Format(
503-
CultureInfo.InvariantCulture,
504-
"RequestPath:{0} RequestId:{1}",
505-
_path,
506-
_traceIdentifier);
507-
}
508-
509-
return _cachedToString;
510-
}
511-
512-
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
513-
{
514-
for (var i = 0; i < Count; ++i)
515-
{
516-
yield return this[i];
517-
}
518-
}
519-
520-
IEnumerator IEnumerable.GetEnumerator()
521-
{
522-
return GetEnumerator();
523-
}
524-
}
525-
}
526453
}

0 commit comments

Comments
 (0)