How to add a parameter to .Net Core structured logging without referencing it in a message?

4k Views Asked by At

I can do this in .net core

_logger.LogInformation("Token validated {clientId}", "MyId");

And then logging libraries like NLog will know that there is a property called clientId with the value MyId in the message and can render it in a special way.

I am trying to do the same without including the property in the message itself, but cannot manage to nail it. This is what I have done so far and it does not result in a property in NLog:

 LogEventInfo info = new LogEventInfo
 {
    Properties = {{"clientId", "MyId"}},
 };
 _logger.Log(Microsoft.Extensions.Logging.LogLevel.Information, "Token validated", info, null, info.MessageFormatter);

This results in a message without property. Is there a better way to do this or have I done something wrong?

1

There are 1 best solutions below

4
On BEST ANSWER

The whole idea with Microsoft-Extension-Logging (MEL) ILogger-interface is not being dependent on a specific Logging-Framework.

If you start creating NLog LogEventInfo-objects, then you might as well call NLog.LogManager.GetCurrentClassLogger() and use that as Logger.

But maybe this wiki-page can give you some ideas:

https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-properties-with-Microsoft-Extension-Logging