diff --git a/src/DefaultBuilder/src/WebHost.cs b/src/DefaultBuilder/src/WebHost.cs
index e8f00fa602ef..2dda5979f84d 100644
--- a/src/DefaultBuilder/src/WebHost.cs
+++ b/src/DefaultBuilder/src/WebHost.cs
@@ -189,12 +189,18 @@ public static IWebHostBuilder CreateDefaultBuilder(string[] args)
config.AddCommandLine(args);
}
})
- .ConfigureLogging((hostingContext, logging) =>
+ .ConfigureLogging((hostingContext, loggingBuilder) =>
{
- logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
- logging.AddConsole();
- logging.AddDebug();
- logging.AddEventSourceLogger();
+ loggingBuilder.Configure(options =>
+ {
+ options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
+ | ActivityTrackingOptions.TraceId
+ | ActivityTrackingOptions.ParentId;
+ });
+ loggingBuilder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
+ loggingBuilder.AddConsole();
+ loggingBuilder.AddDebug();
+ loggingBuilder.AddEventSourceLogger();
}).
UseDefaultServiceProvider((context, options) =>
{
diff --git a/src/Hosting/Hosting/src/Internal/ActivityExtensions.cs b/src/Hosting/Hosting/src/Internal/ActivityExtensions.cs
deleted file mode 100644
index fa23a5ca12b0..000000000000
--- a/src/Hosting/Hosting/src/Internal/ActivityExtensions.cs
+++ /dev/null
@@ -1,43 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System.Diagnostics;
-
-namespace Microsoft.AspNetCore.Hosting
-{
- ///
- /// Helpers for getting the right values from Activity no matter the format (w3c or hierarchical)
- ///
- internal static class ActivityExtensions
- {
- public static string GetSpanId(this Activity activity)
- {
- return activity.IdFormat switch
- {
- ActivityIdFormat.Hierarchical => activity.Id,
- ActivityIdFormat.W3C => activity.SpanId.ToHexString(),
- _ => null,
- } ?? string.Empty;
- }
-
- public static string GetTraceId(this Activity activity)
- {
- return activity.IdFormat switch
- {
- ActivityIdFormat.Hierarchical => activity.RootId,
- ActivityIdFormat.W3C => activity.TraceId.ToHexString(),
- _ => null,
- } ?? string.Empty;
- }
-
- public static string GetParentId(this Activity activity)
- {
- return activity.IdFormat switch
- {
- ActivityIdFormat.Hierarchical => activity.ParentId,
- ActivityIdFormat.W3C => activity.ParentSpanId.ToHexString(),
- _ => null,
- } ?? string.Empty;
- }
- }
-}
diff --git a/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs b/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
index 41b93ac083c3..41156eb7d6e4 100644
--- a/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
+++ b/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
@@ -69,7 +69,7 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
// Scope may be relevant for a different level of logging, so we always create it
// see: https://github.com/aspnet/Hosting/pull/944
// Scope can be null if logging is not on.
- context.Scope = _logger.RequestScope(httpContext, context.Activity);
+ context.Scope = _logger.RequestScope(httpContext);
if (_logger.IsEnabled(LogLevel.Information))
{
diff --git a/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs b/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
index e5fb4fd0c6e6..4f8c1e6e7ec2 100644
--- a/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
+++ b/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
@@ -4,7 +4,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Microsoft.AspNetCore.Http;
@@ -14,9 +13,9 @@ namespace Microsoft.AspNetCore.Hosting
{
internal static class HostingLoggerExtensions
{
- public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext, Activity activity)
+ public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext)
{
- return logger.BeginScope(new HostingLogScope(httpContext, activity));
+ return logger.BeginScope(new HostingLogScope(httpContext));
}
public static void ApplicationError(this ILogger logger, Exception exception)
@@ -97,7 +96,6 @@ private class HostingLogScope : IReadOnlyList>
{
private readonly string _path;
private readonly string _traceIdentifier;
- private readonly Activity _activity;
private string _cachedToString;
@@ -105,7 +103,7 @@ public int Count
{
get
{
- return 5;
+ return 2;
}
}
@@ -121,31 +119,17 @@ public KeyValuePair this[int index]
{
return new KeyValuePair("RequestPath", _path);
}
- else if (index == 2)
- {
- return new KeyValuePair("SpanId", _activity.GetSpanId());
- }
- else if (index == 3)
- {
- return new KeyValuePair("TraceId", _activity.GetTraceId());
- }
- else if (index == 4)
- {
- return new KeyValuePair("ParentId", _activity.GetParentId());
- }
throw new ArgumentOutOfRangeException(nameof(index));
}
}
- public HostingLogScope(HttpContext httpContext, Activity activity)
+ public HostingLogScope(HttpContext httpContext)
{
_traceIdentifier = httpContext.TraceIdentifier;
_path = (httpContext.Request.PathBase.HasValue
? httpContext.Request.PathBase + httpContext.Request.Path
: httpContext.Request.Path).ToString();
-
- _activity = activity;
}
public override string ToString()
@@ -154,12 +138,9 @@ public override string ToString()
{
_cachedToString = string.Format(
CultureInfo.InvariantCulture,
- "RequestPath:{0} RequestId:{1}, SpanId:{2}, TraceId:{3}, ParentId:{4}",
+ "RequestPath:{0} RequestId:{1}",
_path,
- _traceIdentifier,
- _activity.GetSpanId(),
- _activity.GetTraceId(),
- _activity.GetParentId());
+ _traceIdentifier);
}
return _cachedToString;
diff --git a/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs b/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
index 08e3832d7ede..3ee8fbb34662 100644
--- a/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
+++ b/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
@@ -40,50 +40,6 @@ public void CreateContextWithDisabledLoggerDoesNotCreateActivity()
Assert.Null(Activity.Current);
}
- [Fact]
- public void CreateContextWithEnabledLoggerCreatesActivityAndSetsActivityInScope()
- {
- // Arrange
- var logger = new LoggerWithScopes(isEnabled: true);
- var hostingApplication = CreateApplication(out var features, logger: logger);
-
- // Act
- var context = hostingApplication.CreateContext(features);
-
- Assert.Single(logger.Scopes);
- var pairs = ((IReadOnlyList>)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value);
- Assert.Equal(Activity.Current.Id, pairs["SpanId"].ToString());
- Assert.Equal(Activity.Current.RootId, pairs["TraceId"].ToString());
- Assert.Equal(string.Empty, pairs["ParentId"]?.ToString());
- }
-
- [Fact]
- public void CreateContextWithEnabledLoggerAndRequestIdCreatesActivityAndSetsActivityInScope()
- {
- // Arrange
-
- // Generate an id we can use for the request id header (in the correct format)
- var activity = new Activity("IncomingRequest");
- activity.Start();
- var id = activity.Id;
- activity.Stop();
-
- var logger = new LoggerWithScopes(isEnabled: true);
- var hostingApplication = CreateApplication(out var features, logger: logger, configure: context =>
- {
- context.Request.Headers["Request-Id"] = id;
- });
-
- // Act
- var context = hostingApplication.CreateContext(features);
-
- Assert.Single(logger.Scopes);
- var pairs = ((IReadOnlyList>)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value);
- Assert.Equal(Activity.Current.Id, pairs["SpanId"].ToString());
- Assert.Equal(Activity.Current.RootId, pairs["TraceId"].ToString());
- Assert.Equal(id, pairs["ParentId"].ToString());
- }
-
[Fact]
public void ActivityStopDoesNotFireIfNoListenerAttachedForStart()
{