2
votes

I'm working on application which is making use of NLog to allow us to Trace executions of a workflow. Each execution is designated a 'tracking id' (GUID) which is retained through to tie together messages composed and sent. We would ideally like to log each execution out to it's own log file using this tracking id in the name. e.g.

Logs\MyExecution_97301078-2119-48e9-876d-64002257b399.txt
Logs\MyExecution_1e5b60d5-95b5-4fdc-94cc-886d379e2b13.txt
Logs\MyExecution_07b427a9-106d-4865-a288-625e1cd56dd1.txt

Is this achievable using the NLog targets?

1
Hi this example appears to call out explicit names for the separate log files, I was hoping to create log files with names determined by unique Ids, unknown at compile time.Paddy
How are you keeping track of the GUID? Are you explicitly including it in each logging message? Are you setting it in one of the NLOG context objects? Are you using the built-in .NET System.Diagnostics.Trace.CorrelationManager.ActivityId?wageoghe

1 Answers

2
votes

You aren't restricted to registering multiple loggers in a configuration file; you can use the Configuration API. For example (taken from the link, you will probably have to edit for your situation)

// create configuration object 
var config = new LoggingConfiguration();

// create a target
var fileTarget = new FileTarget();
config.AddTarget("SomeName", fileTarget);

// set properties
fileTarget.FileName = "something/guid.txt";

// activate the configuration
LogManager.Configuration = config;