Skip to content

Add Tags and Baggage to LogScope using ActivityTrackingOptions #46571

Closed

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.

Activity

  1. added
    api-suggestion
    on Jan 5, 2021
  2. Dotnet-GitSync-Bot commented on Jan 5, 2021

    @Dotnet-GitSync-Bot
    Collaborator

    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.

  3. added
    untriaged
    on Jan 5, 2021
  4. msallin commented on Jan 5, 2021

    @msallin
    ContributorAuthor

    area label: area-Extensions-Logging

  5. msallin commented on Jan 28, 2021

    @msallin
    ContributorAuthor
  6. removed
    untriaged
    on Jan 28, 2021
  7. added this to the 6.0.0 milestone on Jan 28, 2021
  8. tarekgh commented on Jan 28, 2021

    @tarekgh
    Member

    @msallin

    Tags now are string-object pairs and not string-string. What you suggest in formatting the tags? just call ToString on the value? or need to provide custom formatter for that?

    CC @noahfalk @maryamariyan

  9. tarekgh commented on Jan 28, 2021

    @tarekgh
    Member
  10. msallin commented on Jan 29, 2021

    @msallin
    ContributorAuthor

    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 overwrite ToString .

  11. tarekgh commented on Feb 12, 2021

    @tarekgh
    Member

    @davidfowl @shirhatti the proposal looks reasonable to me. Do you have any concern bout it before we move forward?

  12. davidfowl commented on Feb 12, 2021

    @davidfowl
    Member

    As long as it's not on by default, looks good.

  13. added
    api-ready-for-review
    and removed
    api-suggestion
    on Feb 12, 2021
  14. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 23, 2021
  15. terrajobst commented on Feb 23, 2021

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace Microsoft.Extensions.Logging
    {
        public partial enum ActivityTrackingOptions
        {
            Tags        = 0x0020,
            Baggage     = 0x0040,
        }
    }
  16. msallin commented on Feb 23, 2021

    @msallin
    ContributorAuthor

    I would love to contribute this tiny part to .NET
    Let me know when/if you are ready to accept a PR.

  17. tarekgh commented on Feb 23, 2021

    @tarekgh
    Member

    @msallin go ahead and submit the PR and tag me there. Thanks for willing to help with that.

  18. tarekgh commented on Feb 24, 2021

    @tarekgh
    Member

    @msallin please let me know if you need any help or have any question. Thanks!

  19. tarekgh commented on Mar 12, 2021

    @tarekgh
    Member

    Fixed by #48722

  20. ghost locked as resolved and limited conversation to collaborators on Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-Extensions-Logging

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions