Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout ILogger scopes (ILogger.BeginScope) ? #78

Closed
GreenMoose2 opened this issue Dec 21, 2016 · 10 comments
Closed

Layout ILogger scopes (ILogger.BeginScope) ? #78

GreenMoose2 opened this issue Dec 21, 2016 · 10 comments
Labels

Comments

@GreenMoose2
Copy link

v1.0.0-rtm-alpha5

Hi,

Is there any way I can render ILogger scopes (ILogger.BeginScope) in the NLog layout configuration for dotnetcore?

Thanks!

@304NotModified
Copy link
Member

Hey! could you give an example?

@GreenMoose2
Copy link
Author

GreenMoose2 commented Jan 11, 2017

Hi, see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging in section "Log scopes", example from link:

public IActionResult GetById(string id)
{
    TodoItem item;
    using (_logger.BeginScope("Message attached to logs created in the using block"))
    {
        _logger.LogInformation(LoggingEvents.GET_ITEM, "Getting item {ID}", id);
        item = _todoRepository.Find(id);
        if (item == null)
        {
            _logger.LogWarning(LoggingEvents.GET_ITEM_NOTFOUND, "GetById({ID}) NOT FOUND", id);
            return NotFound();
        }
    }
    return new ObjectResult(item);
}

@304NotModified
Copy link
Member

${ndc},

for more options: https://github.com/NLog/NLog/wiki/Ndc-Layout-Renderer

@304NotModified
Copy link
Member

I assume your question has been answered, if not, let us know!

@sliekens
Copy link

@304NotModified is there any additional config needed to make this work?

What I have so far:

  • a call to UseNLog()
  • an nlog target that has ${ndc} in its layout
  • code that does ILogger.BeginScope(string)

But my output has empty strings where the ndc should be.

@304NotModified
Copy link
Member

304NotModified commented Aug 23, 2018

${ndc} was pre-1.0 of NLog.Extensions.Logging

It should be ${ndlc} now. - https://github.com/NLog/NLog/wiki/Ndlc-Layout-Renderer

See also some examples in the unit tests

@snakefoot
Copy link
Contributor

@304NotModified
Copy link
Member

@sliekens
Copy link

Thanks for the links.

@dlidstrom
Copy link

dlidstrom commented Dec 2, 2022

New (in V5) is to use ${scopenested}. See https://github.com/NLog/NLog/wiki/ScopeNested-Layout-Renderer.

And ${scopeproperty}, See https://github.com/NLog/NLog/wiki/ScopeProperty-Layout-Renderer

And ${all-event-properties:includeScopeProperties=true}, See https://github.com/NLog/NLog/wiki/All-Event-Properties-Layout-Renderer

And JsonLayout with IncludeScopeProperties = true, see https://github.com/NLog/NLog/wiki/JsonLayout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants