Benjamin Roelandt Benjamin Roelandt - 2 months ago 11
C# Question

Nlog - Adding a deepness data

I'm currently working on a custom installer, and a lot of my company's softwares use a specific log format (and we have a lot of tools to analyse them) but all of their programs are written in Delphi and the project i'm working on use C# so i can't reuse directly the library.

So i'm trying to format the NLog's output to fit our format while still having a "standard" output and being able to output the logs directly in a WPF control.

We have all the usual log level (debug, info, warning, error, ...) but we also have deepness mechanism. Basically we can have things like:

log.Info("Hey");
log.BeginBlock("Some logs");
for(....)
{
instructions;
log.Error("...");
}
log.EndBlock();
log.Info(".");


Basically the output will be:

Time Deepness Message Warning Error
[TIME] 1 Hey 0 0
[TIME] 1 Some logs 0 0
[TIME] 2 ... 0 1
[TIME] 1 . 0 0


So i'm wondering if I can do something like that with NLog or if i should consider another Log framework

thanks !

Amy Amy
Answer

Take a look at the Mapped Diagnostics Context layout renderer. It lets you add contextual information that flows along with your code. Very useful.

Use the Mapped Diagnostics Context when you have information that you want available to every logger executing on the current thread. As the Mapped Diagnostics Context is thread-local, you must configure all properties within the context of each thread where the property value is required.

Example usage:

MappedDiagnosticsContext.Set("PropertyName", "PropertyValue");

Then in the log config:

${mdc:item=PropertyName}
Comments