question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonConsoleLogger and SimpleConsoleLogger do not support BeginScope

See original GitHub issue

I wrote the following code and was expecting, that the logger will write the scope keys (in my example key, JobId and fileName inside the logging. When I checked the logging on the UI the keys are not written.

using (this._logger.BeginScope(new Dictionary<string, object> { ["key"] = "1234567890123", ["jobId"] = 54 }))
{
    this._logger.LogTrace("Test Log - Scope Map - Trace");
    this._logger.LogDebug("Test Log - Scope Map - Debug");
    this._logger.LogInformation("Test Log - Scope Map - Information");
    this._logger.LogWarning("Test Log - Scope Map - Warning");
    this._logger.LogError("Test Log - Scope Map - Error");
    this._logger.LogError(new Exception("My Error Exception"), "Test Log - Scope Map - Error with Exception");
    this._logger.LogCritical("Test Log - Scope Map - Critical");
    this._logger.LogCritical(new Exception("My Critical Exception"), "Test Log - Scope Map - Critical with Exception");

    using (this._logger.BeginScope(new Dictionary<string, object> { ["fileName"] = "Test.txt" }))
    {
        this._logger.LogTrace("Test Log - Scope File - Trace");
        this._logger.LogDebug("Test Log - Scope File - Debug");
        this._logger.LogInformation("Test Log - Scope File - Information");
        this._logger.LogWarning("Test Log - Scope File - Warning");
        this._logger.LogError("Test Log - Scope File - Error");
        this._logger.LogError(new Exception("My Error Exception"), "Test Log - Scope File - Error with Exception");
        this._logger.LogCritical("Test Log - Scope File - Critical");
        this._logger.LogCritical(new Exception("My Critical Exception"), "Test Log - Scope File - Critical with Exception");
    }
}

Maybe it could be an option to include a third logger in the framework, where an output format string can be set via dependency injection. Inside this format string, the scope properties could be used when they are set.

Example: “{scope:jobId} {scope:key} {message}”

Alternatively the logger could write the scope to the JsonPayload.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jskeetcommented, Jan 26, 2021

Related: https://github.com/dotnet/extensions/issues/1883

(In fact, writing the scopes to a separate line in the output, in the same way that we do for exceptions, might make it sufficiently non-chatty… same amount of information, but clearer. Will give that a go.)

0reactions
jskeetcommented, Jan 27, 2021

Right, this will be in the next release - I’m going to see which other issues I can pick off before we do a release though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Console log formatting - .NET
When using the Json console formatter, do not pass in log messages that have already been serialized as JSON. The logging infrastructure itself ......
Read more >
Logger Begin Scope is not working for me! .net Core
If you want to see the content in your BeginScope , you must to structured logging first.You can see more details in this...
Read more >
NLog 5.0 - List of major changes
BeginScope but no logevents are written. ... NLog Configuration Variables now has support for other types of Layouts than SimpleLayout .
Read more >
How to include scopes when logging exceptions in ASP. ...
This post describes how to work around an issue I ran into when logging exceptions that occur inside a scope block in ASP.NET...
Read more >
Using The ILogger BeginScope In ASP.NET Core
To turn on scopes, first you need to actually be using a logger that implements them – not all do. And secondly you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found