Repository navigation
Description
Background and Motivation
Since .NET 5 the LoggerFactoryScopeProvider can be configured using the ActivityTrackingOptions, to add the SpanId, TraceId, ParentId, TraceStateString and TraceFlags to the scope. The SpanId, TraceId and ParentId are added by default when using Host.CreateDefaultBuilder().
In our use case, we implement a own ConsoleFormatter which writes the log lines as key-value-pairs, to be able to easy search them with Splunk. We want to add the SpandId, TraceId, ParentId and all items from baggage and tags to each log line.
2021-55-05 08:01:01.075 level=INFO eventName=RequestFinished eventId=2 sourceContext="Microsoft.AspNetCoreHosting.Diagnostics" message="Request finished HTTP/1.1 GET http://localhost/ - - - 200- - 101.8717ms" ElapsedMilliseconds="101.8717" StatusCode="200" ContentType="" ContentLength="" Protocol="HTTP/1.1" Method="GET" Scheme="http" Host="localhost" PathBase="" Path="/" QueryString="" SpanId="976b6fa436d88a4e" TraceId="a23f2582c9522c42bce68662015ed2f1" ParentId="0000000000000000" RequestId="0HM5HAUAVSI9J" RequestPath="/"
added some more text to the proposal code by @tarekgh
Proposed API
using System;
namespace Microsoft.Extensions.Logging
{
[Flags]
public enum ActivityTrackingOptions
{
None = 0x0000,
SpanId = 0x0001,
TraceId = 0x0002,
ParentId = 0x0004,
TraceState = 0x0008,
TraceFlags = 0x0010,
+ Tags = 0x0020,
+ Baggage = 0x0040,
}
}Usage Examples
namespace Microsoft.Extensions.Hosting
{
public static class Host
{
public static IHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new HostBuilder();
builder.UseContentRoot(Directory.GetCurrentDirectory());
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
//...
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.Configure(options =>
{
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
| ActivityTrackingOptions.TraceId
| ActivityTrackingOptions.ParentId
+ | ActivityTrackingOptions.Baggage
+ | ActivityTrackingOptions.Tags;
});
})
return builder;
}
}
}Alternative Designs
- Make it possible to subclass
LoggerFactoryScopeProviderand add the behavior this way. - Use another Enum because
ActivityTrackingOptionscurrently represents only parts of the W3C spec
Risks
No risks.
Activity
Dotnet-GitSync-Bot commentedon Jan 5, 2021
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
msallin commentedon Jan 5, 2021
area label: area-Extensions-Logging
msallin commentedon Jan 5, 2021
Releated issues:
msallin commentedon Jan 28, 2021
CC: @tarekgh @shirhatti
tarekgh commentedon Jan 28, 2021
@msallin
Tags now are string-object pairs and not string-string. What you suggest in formatting the tags? just call
ToStringon the value? or need to provide custom formatter for that?CC @noahfalk @maryamariyan
tarekgh commentedon Jan 28, 2021
CC @davidfowl
msallin commentedon Jan 29, 2021
In our use case we attach only strings as tags. Thus, its fine to just use
ToString. Using a formatter would be useful if one adds objects which one does not have control. Else one could still overwriteToString.tarekgh commentedon Feb 12, 2021
@davidfowl @shirhatti the proposal looks reasonable to me. Do you have any concern bout it before we move forward?
davidfowl commentedon Feb 12, 2021
As long as it's not on by default, looks good.
terrajobst commentedon Feb 23, 2021
Video
msallin commentedon Feb 23, 2021
I would love to contribute this tiny part to .NET
Let me know when/if you are ready to accept a PR.
tarekgh commentedon Feb 23, 2021
@msallin go ahead and submit the PR and tag me there. Thanks for willing to help with that.
tarekgh commentedon Feb 24, 2021
@msallin please let me know if you need any help or have any question. Thanks!
Baggage and Tags for activity tracking options (#46571) (#48722)
tarekgh commentedon Mar 12, 2021
Fixed by #48722