Skip to content

Commit f809e23

Browse files
committed
Revert "Hoist activity fields to the logging scope (#11211)"
This reverts commit f7a2d3c.
1 parent eab9005 commit f809e23

File tree

3 files changed

+10
-65
lines changed

3 files changed

+10
-65
lines changed

src/Hosting/Hosting/src/Internal/ActivityExtensions.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
6969
// Scope may be relevant for a different level of logging, so we always create it
7070
// see: https://github.com/aspnet/Hosting/pull/944
7171
// Scope can be null if logging is not on.
72-
context.Scope = _logger.RequestScope(httpContext, context.Activity);
72+
context.Scope = _logger.RequestScope(httpContext, context.Activity.Id);
7373

7474
if (_logger.IsEnabled(LogLevel.Information))
7575
{

src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7-
using System.Diagnostics;
87
using System.Globalization;
98
using System.Reflection;
109
using Microsoft.AspNetCore.Http;
@@ -14,9 +13,9 @@ namespace Microsoft.AspNetCore.Hosting
1413
{
1514
internal static class HostingLoggerExtensions
1615
{
17-
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext, Activity activity)
16+
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext, string activityId)
1817
{
19-
return logger.BeginScope(new HostingLogScope(httpContext, activity));
18+
return logger.BeginScope(new HostingLogScope(httpContext, activityId));
2019
}
2120

2221
public static void ApplicationError(this ILogger logger, Exception exception)
@@ -97,15 +96,15 @@ private class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
9796
{
9897
private readonly string _path;
9998
private readonly string _traceIdentifier;
100-
private readonly Activity _activity;
99+
private readonly string _activityId;
101100

102101
private string _cachedToString;
103102

104103
public int Count
105104
{
106105
get
107106
{
108-
return 5;
107+
return 3;
109108
}
110109
}
111110

@@ -123,29 +122,20 @@ public KeyValuePair<string, object> this[int index]
123122
}
124123
else if (index == 2)
125124
{
126-
return new KeyValuePair<string, object>("SpanId", _activity.GetSpanId());
127-
}
128-
else if (index == 3)
129-
{
130-
return new KeyValuePair<string, object>("TraceId", _activity.GetTraceId());
131-
}
132-
else if (index == 4)
133-
{
134-
return new KeyValuePair<string, object>("ParentId", _activity.GetParentId());
125+
return new KeyValuePair<string, object>("ActivityId", _activityId);
135126
}
136127

137128
throw new ArgumentOutOfRangeException(nameof(index));
138129
}
139130
}
140131

141-
public HostingLogScope(HttpContext httpContext, Activity activity)
132+
public HostingLogScope(HttpContext httpContext, string activityId)
142133
{
143134
_traceIdentifier = httpContext.TraceIdentifier;
144135
_path = (httpContext.Request.PathBase.HasValue
145136
? httpContext.Request.PathBase + httpContext.Request.Path
146137
: httpContext.Request.Path).ToString();
147-
148-
_activity = activity;
138+
_activityId = activityId;
149139
}
150140

151141
public override string ToString()
@@ -154,12 +144,10 @@ public override string ToString()
154144
{
155145
_cachedToString = string.Format(
156146
CultureInfo.InvariantCulture,
157-
"RequestPath:{0} RequestId:{1}, SpanId:{2}, TraceId:{3}, ParentId:{4}",
147+
"RequestPath:{0} RequestId:{1}, ActivityId:{2}",
158148
_path,
159149
_traceIdentifier,
160-
_activity.GetSpanId(),
161-
_activity.GetTraceId(),
162-
_activity.GetParentId());
150+
_activityId);
163151
}
164152

165153
return _cachedToString;

0 commit comments

Comments
 (0)