Get the layout from the target in NLog

619 Views Asked by At

What I have a NLog configuration file.

LoggingConfiguration nLogLoggingConfiguration;

Then

foreach (var target in nLogLoggingConfiguration.AllTargets)
        {

I want to get the layout from the target, it seems in the memory. When I hover over it in the debugging time. I found: target

What I want is

 ${message}|${exception:format=tostring}|${stacktrace}
1

There are 1 best solutions below

3
On BEST ANSWER

Not all targets have layouts, so you have to get the items that inherit from TargetWithLayout, Then you can call ToString on the Layout object. An example:

foreach (var target in nLogLoggingConfiguration.AllTargets.OfType<TargetWithLayout>())
{
    string layoutString = target.Layout.ToString();
    Console.WriteLine(layoutString);
}