I have a project which already uses NLog . The application creates an instance of a given class passes an instance of ILogger to it and then calls an "execute" method within the instance - The system is multi threaded and can have up to 200 of these instances running at once, the log files can get big and difficult to read.
We have a requirement to create a new log file per instance of a class and store it with the output of that instance (they each have a unique identifier)
My question is: is it possible to have NLog create a new file per instance of a class? I know you can have different log files for different classes, what I want is a different log file for each instance of a class.
I have looked online but cant find much information on this, I dont want to reinvent the wheel by creating my own logging class if its already catered for in NLog.
Below is a console app that I have knocked up to demonstrate what happens when reconfiguring the existing loggers - essentially it changes all instances of the logger
class Program
{
static void Main(string[] args)
{
BackgroundWorker bw1 = new BackgroundWorker();
bw1.DoWork += Bw1_DoWork;
bw1.RunWorkerAsync();
BackgroundWorker bw2 = new BackgroundWorker();
bw2.DoWork += Bw2_DoWork;
bw2.RunWorkerAsync();
Console.ReadLine();
}
private static void Bw1_DoWork(object sender, DoWorkEventArgs e)
{
workLoad one = new workLoad("One");
one.execute();
}
private static void Bw2_DoWork(object sender, DoWorkEventArgs e)
{
workLoad one = new workLoad("Two");
one.execute();
}
}
public class workLoad
{
private ILogger _logger { get; set; }
private string _number { get; set; }
public workLoad(string number)
{
_logger = LogManager.GetCurrentClassLogger();
_number = number;
var target = (FileTarget) LogManager.Configuration.FindTargetByName("DebugFile");
target.FileName = $"c:\\temp\\File{number}.txt";
LogManager.ReconfigExistingLoggers();
}
public void execute()
{
for (int i = 0; i < 1000; i++)
{
_logger.Info(_number + " LOOPING" + i);
}
}
}
this results in all output going to one file (two.txt) a sample of the log file is below.
2017-06-13 17:00:42.4156 TestNlog.workLoad Two LOOPING0 2017-06-13 17:00:42.4156 TestNlog.workLoad One LOOPING0 2017-06-13 17:00:42.4806 TestNlog.workLoad One LOOPING1 2017-06-13 17:00:42.4746 TestNlog.workLoad Two LOOPING1 2017-06-13 17:00:42.4806 TestNlog.workLoad One LOOPING2 2017-06-13 17:00:42.4806 TestNlog.workLoad Two LOOPING2 2017-06-13 17:00:42.4946 TestNlog.workLoad One LOOPING3 2017-06-13 17:00:42.4946 TestNlog.workLoad Two LOOPING3 2017-06-13 17:00:42.4946 TestNlog.workLoad One LOOPING4 2017-06-13 17:00:42.4946 TestNlog.workLoad Two LOOPING4 2017-06-13 17:00:42.5132 TestNlog.workLoad One LOOPING5 2017-06-13 17:00:42.5132 TestNlog.workLoad Two LOOPING5 2017-06-13 17:00:42.5132 TestNlog.workLoad One LOOPING6 2017-06-13 17:00:42.5257 TestNlog.workLoad Two LOOPING6 2017-06-13 17:00:42.5257 TestNlog.workLoad One LOOPING7 2017-06-13 17:00:42.5257 TestNlog.workLoad Two LOOPING7 2017-06-13 17:00:42.5407 TestNlog.workLoad Two LOOPING8 2017-06-13 17:00:42.5407 TestNlog.workLoad One LOOPING8 2017-06-13 17:00:42.5407 TestNlog.workLoad Two LOOPING9 2017-06-13 17:00:42.5407 TestNlog.workLoad One LOOPING9 2017-06-13 17:00:42.5577 TestNlog.workLoad Two LOOPING10 2017-06-13 17:00:42.5577 TestNlog.workLoad One LOOPING10
Ideally Id be looking for everything from instance one to go into One.TXT and everything from instance two to be in two.txt (You can see how having 200+ running like this might be a problem!)