How can I enable the following in NLog that produces the shown output:
using (_logger.BeginScope("Starting processing"))
{
_logger.LogInformation("Algorithm1 Initalized");
_logger.LogInformation("Algorithm2 Initalized");
using (_logger.BeginScope("Algorithm 1 running"))
{
for (int i=0; i<5; i++)
_logger.LogInformation($"Algorithm1 progress {i}");
}
using (_logger.BeginScope("Algorithm 2 running"))
{
for (int i = 0; i < 5; i++)
_logger.LogInformation($"Algorithm2 progress {i}");
}
}
The output I'd like to have is this:
Starting processing
Algorithm1 Initialized
Algorithm2 Initialized
Algorithm 1 running
Algorithm1 0
Algorithm1 1
Algorithm1 2
Algorithm1 3
Algorithm1 4
Algorithm 1 finished in 10secs
Algorithm 2 running
Algorithm2 0
Algorithm2 1
Algorithm2 2
Algorithm2 3
Algorithm2 4
Algorithm 2 finished in 10secs
Processing Finished
Explanation: For each scope I want to create a headline (e.g "Starting processing") that records the time the scope was created. Additionally when the scope ends, I want to output a footer that also reports the duration the scope was "open".
The closing footer is not written in the code example above, because I don't know how to do it. I could create my own BeginScope IDisposable that simply wraps the dispose, but maybe there is a better way?
Additionally, I want to indent all text according to the depth of the scope.
I have found the ${scopetiming} and ${scopecontext}, but I honestly don't know how to apply them.
Any help would be appreciated ;-)
NLog.LogManager.GetCurrentClassLogger()
and ${logger}. See also: https://mcmap.net/q/1176288/-nlog-feature-question-can-the-stack-depth-be-taken-into-account-while-writing-logs – Burgrave