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

Add context on logger level #1615

Closed
304NotModified opened this issue Sep 3, 2016 · 5 comments
Closed

Add context on logger level #1615

304NotModified opened this issue Sep 3, 2016 · 5 comments
Labels

Comments

@304NotModified
Copy link
Member

Feature:

We like to do:

var logger = LogManager.GetCurrentClassLogger();
logger.AddContext("declarationId", 10);

rendering, something like this:

LDC? "logger diagnostic context"

${ldc}
@poma
Copy link

poma commented Sep 6, 2016

Logger already provides us with a class name when we render a message. But what if I want to identify which instance of the class a message comes from?

I think it would be very useful if this feature allowed us to attach a context data that was visible only for messages logged from a certain instance of the class. So we could attach some data that identifies a class instance in a readable way.

I'm not sure how it can be implemented though since a Logger is usually a static member and we never pass this reference to any logging methods. Or maybe this is a separate feature request altogether?

@poma
Copy link

poma commented Sep 6, 2016

By the way does LogManager.GetCurrentClassLogger() return the same instance when called multiple times? Because if it isn't then we can just declare Logger as an instance field and attach a class instance identifier to each logger.

@304NotModified
Copy link
Member Author

304NotModified commented Sep 7, 2016

I'm not sure how it can be implemented though since a Logger is usually a static member

Logger isn't static? LogMananger is

By the way does LogManager.GetCurrentClassLogger() return the same instance when called multiple times?

it's cached by name, for performance reasons

I think it would be very useful if this feature allowed us to attach a context data that was visible only for messages logged from a certain instance of the class. So we could attach some data that identifies a class instance in a readable way.

You mean that the context data should be cleared when the Logger instance is out of scope? Manually resetting isn't a good solution?

@poma
Copy link

poma commented Sep 7, 2016

I was hoping to achieve something like this:

2016-09-07 18:34:01.1508|INFO|App.BusinessLogic.Transaction(transactionId=42)|Started processing client payment
2016-09-07 18:34:01.1508|INFO|App.BusinessLogic.Transaction(transactionId=1337)|Started processing client payment
2016-09-07 18:34:01.1508|INFO|App.BusinessLogic.Transaction(transactionId=42)|Sending request to a payment gateway

So that there is a way to distinguish between messages emitted by a different class instances and probably to route them to different log files if I want to. After some googling I figured out that I can just construct a differently named Logger for each class instance (I'm new to NLog). But when transactionId is embedded in Logger name string it feels that I'm doing it wrong.

@304NotModified
Copy link
Member Author

Another work around is to use the MLDC and prefix with the logger /class name?

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

2 participants