Skip to content

Commit c97a002

Browse files
authored
Use implicit logging scope for Activity (#22376)
* Revert "Hoist activity fields to the logging scope (#11211)" This reverts commit f7a2d3c. * Remove tests with Activity * Remove ActivityId from HostingLogScope * Enable propogation in CreateDefaultBuilder * Clean up * s/logging/loggingBuilder * Enable Activity propogation for generichost * Replace with runtime/pull/37892
1 parent 36be16b commit c97a002

File tree

5 files changed

+18
-118
lines changed

5 files changed

+18
-118
lines changed

src/DefaultBuilder/src/WebHost.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@ public static IWebHostBuilder CreateDefaultBuilder(string[] args)
189189
config.AddCommandLine(args);
190190
}
191191
})
192-
.ConfigureLogging((hostingContext, logging) =>
192+
.ConfigureLogging((hostingContext, loggingBuilder) =>
193193
{
194-
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
195-
logging.AddConsole();
196-
logging.AddDebug();
197-
logging.AddEventSourceLogger();
194+
loggingBuilder.Configure(options =>
195+
{
196+
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
197+
| ActivityTrackingOptions.TraceId
198+
| ActivityTrackingOptions.ParentId;
199+
});
200+
loggingBuilder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
201+
loggingBuilder.AddConsole();
202+
loggingBuilder.AddDebug();
203+
loggingBuilder.AddEventSourceLogger();
198204
}).
199205
UseDefaultServiceProvider((context, options) =>
200206
{

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);
7373

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

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

Lines changed: 6 additions & 25 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)
1817
{
19-
return logger.BeginScope(new HostingLogScope(httpContext, activity));
18+
return logger.BeginScope(new HostingLogScope(httpContext));
2019
}
2120

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

102100
private string _cachedToString;
103101

104102
public int Count
105103
{
106104
get
107105
{
108-
return 5;
106+
return 2;
109107
}
110108
}
111109

@@ -121,31 +119,17 @@ public KeyValuePair<string, object> this[int index]
121119
{
122120
return new KeyValuePair<string, object>("RequestPath", _path);
123121
}
124-
else if (index == 2)
125-
{
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());
135-
}
136122

137123
throw new ArgumentOutOfRangeException(nameof(index));
138124
}
139125
}
140126

141-
public HostingLogScope(HttpContext httpContext, Activity activity)
127+
public HostingLogScope(HttpContext httpContext)
142128
{
143129
_traceIdentifier = httpContext.TraceIdentifier;
144130
_path = (httpContext.Request.PathBase.HasValue
145131
? httpContext.Request.PathBase + httpContext.Request.Path
146132
: httpContext.Request.Path).ToString();
147-
148-
_activity = activity;
149133
}
150134

151135
public override string ToString()
@@ -154,12 +138,9 @@ public override string ToString()
154138
{
155139
_cachedToString = string.Format(
156140
CultureInfo.InvariantCulture,
157-
"RequestPath:{0} RequestId:{1}, SpanId:{2}, TraceId:{3}, ParentId:{4}",
141+
"RequestPath:{0} RequestId:{1}",
158142
_path,
159-
_traceIdentifier,
160-
_activity.GetSpanId(),
161-
_activity.GetTraceId(),
162-
_activity.GetParentId());
143+
_traceIdentifier);
163144
}
164145

165146
return _cachedToString;

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,6 @@ public void CreateContextWithDisabledLoggerDoesNotCreateActivity()
4040
Assert.Null(Activity.Current);
4141
}
4242

43-
[Fact]
44-
public void CreateContextWithEnabledLoggerCreatesActivityAndSetsActivityInScope()
45-
{
46-
// Arrange
47-
var logger = new LoggerWithScopes(isEnabled: true);
48-
var hostingApplication = CreateApplication(out var features, logger: logger);
49-
50-
// Act
51-
var context = hostingApplication.CreateContext(features);
52-
53-
Assert.Single(logger.Scopes);
54-
var pairs = ((IReadOnlyList<KeyValuePair<string, object>>)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value);
55-
Assert.Equal(Activity.Current.Id, pairs["SpanId"].ToString());
56-
Assert.Equal(Activity.Current.RootId, pairs["TraceId"].ToString());
57-
Assert.Equal(string.Empty, pairs["ParentId"]?.ToString());
58-
}
59-
60-
[Fact]
61-
public void CreateContextWithEnabledLoggerAndRequestIdCreatesActivityAndSetsActivityInScope()
62-
{
63-
// Arrange
64-
65-
// Generate an id we can use for the request id header (in the correct format)
66-
var activity = new Activity("IncomingRequest");
67-
activity.Start();
68-
var id = activity.Id;
69-
activity.Stop();
70-
71-
var logger = new LoggerWithScopes(isEnabled: true);
72-
var hostingApplication = CreateApplication(out var features, logger: logger, configure: context =>
73-
{
74-
context.Request.Headers["Request-Id"] = id;
75-
});
76-
77-
// Act
78-
var context = hostingApplication.CreateContext(features);
79-
80-
Assert.Single(logger.Scopes);
81-
var pairs = ((IReadOnlyList<KeyValuePair<string, object>>)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value);
82-
Assert.Equal(Activity.Current.Id, pairs["SpanId"].ToString());
83-
Assert.Equal(Activity.Current.RootId, pairs["TraceId"].ToString());
84-
Assert.Equal(id, pairs["ParentId"].ToString());
85-
}
86-
8743
[Fact]
8844
public void ActivityStopDoesNotFireIfNoListenerAttachedForStart()
8945
{

0 commit comments

Comments
 (0)