Giter Club home page Giter Club logo

Comments (3)

YanBrunet avatar YanBrunet commented on May 26, 2024 1

Thank you for taking the time to answer. Although, I wasn't satisfied because I see this issue as a breach of the Liskov Substitution Principle.

Hence, I jumped in the code to find where the issue was originating from.

BeginScope(ILogger, String, Object[]) is an extension from the runtime, and therefore, outside of the scope of this repos.

The main concern that prompted me to open this issue, is the fact that the value {OriginalMessage} is overwritten by whatever is contained in the scope.

I could track down the issue to : source code

The scope values are only added after the state value passed in the Log<TState>() method.
In this case, since the scope used a FormattedLogValues, it also includes a key {OriginalFormat}, which overwrites the one previously created.

To solve the main issue, editing the condition at line 202 of the file as such :

if (item.Key == "{OriginalFormat}")
{
    if (!dict.ContainsKey("OriginalFormat")) 
        dict["OriginalFormat"] = Convert.ToString(item.Value, CultureInfo.InvariantCulture);
}

To solve the secondary issue, but this could have unforseen consequences, knowing that there is an OriginalFormat key, it could be assumed that the activeScope uses some kind of formater and just add it to the builder.

if (item.Key == "{OriginalFormat}")
{
    if (!dict.ContainsKey("OriginalFormat")) 
        dict["OriginalFormat"] = Convert.ToString(item.Value, CultureInfo.InvariantCulture);

    builder.Append(" => ").Append(activeScope);
}

from applicationinsights-dotnet.

cijothomas avatar cijothomas commented on May 26, 2024

using (var scope2 = Logger.BeginScope("Executing {Process} : {Id}", processName, id ))

^ do not use this for Scopes. Instead pass in KVP yourself like below:

        KeyValuePair<string, object>[] scopeValues = [
            new ("Id", id ),
            new( "Process", processName),
        ];
        using (var scope = Logger.BeginScope(scopeValues))
        {

There are no fixes planned for scopes in this repo - please follow the above suggestion. (Even when you move to openTelemetry, still the above recommendation should be followed.)

from applicationinsights-dotnet.

cijothomas avatar cijothomas commented on May 26, 2024

which overwrites the one previously created.

That is true. There kvpairs in the TraceTelemetry cannot accept duplicates. So if there are more than one OriginalFormat key, only one can remain. This would be the case when you has nested scopes as well.

This issue (#2241) shows other possible cases where the information is lost, along with some ideas to fix. But like I said, there won't be any such fixes in this repos, due to the long term strategy of only using OpenTelemetry exclusively.

And I don't see OTel solving it either, it'll remain user responsibility to not use loggermessage templates in scope, but use only list/ienumeralbe/dic in scopes, or potentially move away from scopes to some other enrichment mechanism.

from applicationinsights-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.