Skip to content

Add Tags and Baggage to LogScope using ActivityTrackingOptions #46571

Description

@msallin

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 LoggerFactoryScopeProvider and add the behavior this way.
  • Use another Enum because ActivityTrackingOptions currently represents only parts of the W3C spec

Risks

No risks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions