-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
This issue tracking the work as the part of the issue #31372
Rationale and Use Cases
Currently, there is no automatic way to log the tracing context info with the logging scopes. Tracing context info is the trace Id, Span Id, Parent Id, Trace State, and Trace flags which included inside the Activity object stored in the current execution context. This issue is to support adding the tracing context info with the logging scopes.
Adding Tracing context info to the logging scope will require some memory allocations and also will increase the produced text inside the logs, we are going to provide an opt-in option to enable the new behavior. Although the whole feature is about internal implementation details, we still need to expose some API for the opt-in option. All Proposed APIs here are only to provide the opt-in option for enabling the trace context info in logging and choose which parts of the trace context should be included.
Here is a code sample showing how to opt-in to include the trace context info in the logs.
var loggerFactory = LoggerFactory.Create(builder =>
{
// Enable logging trace context info in the logging scopes.
builder.Configure(factoryOptions => factoryOptions.ActivityTrackingOptions = ActivityTrackingOptions.Default);
});
When enable trace context info logging, will get in the logging results like the following:
info: Microsoft.Extensions.Logging.Test.ConsoleLoggerTest[0]
=> SpanId:|30de9810-4985f5593b3c7f14.1., TraceId:30de9810-4985f5593b3c7f14, ParentId:|30de9810-4985f5593b3c7f14. => { Scope1 = 1 } => { Scope2 = 2 }
Proposed API
- ActiviyTrackingOptions is a flags enum has the values of which parts of the trace context can be included in the logs.
- LoggerFactoryOptions is the new options type for the logger factory. Although it contains the only property ActivityTrackingOptions used to enable the trace context logging, this type can be extended in the future to include more options as needed.
namespace Microsoft.Extensions.Logging
{
[Flags]
public enum ActivityTrackingOptions
{
None = 0x0000,
SpanId = 0x0001,
TraceId = 0x0002,
ParentId = 0x0004,
Default = SpanId | TraceId | ParentId,
TraceState = 0x0008,
TraceFlags = 0x0010
}
public class LoggerFactoryOptions
{
public LoggerFactoryOptions() { }
public ActivityTrackingOptions ActivityTrackingOptions { get {throw null; } set { throw null; } }
}
}
- Adding more constructors to the existing LoggerFactory class. This addition mainly to have the Dependency Injection (DI) work nicely with the introduced LoggerFactoryOptions.
namespace Microsoft.Extensions.Logging
{
// LoggerFactory is existing type
public partial class LoggerFactory : ILoggerFactory, System.IDisposable
{
public LoggerFactory(IOptions<LoggerFactoryOptions> options) { }
public LoggerFactory(IEnumerable<ILoggerProvider> providers, IOptions<LoggerFactoryOptions> options) { }
public LoggerFactory(IEnumerable<ILoggerProvider> providers, LoggerFilterOptions filterOptions, IOptions<LoggerFactoryOptions> options) { }
public LoggerFactory(IEnumerable<ILoggerProvider> providers, IOptionsMonitor<LoggerFilterOptions> filterOption, IOptions<LoggerFactoryOptions> options) { }
}
}
- Adding the extension method Configure to the logger builder
namespace Microsoft.Extensions.Logging
{
// LoggingBuilderExtensions is existing type
public static partial class LoggingBuilderExtensions
{
public static ILoggingBuilder Configure(this ILoggingBuilder builder, Action<LoggerFactoryOptions> action) {...}
}
}