I'm trying to generate targets & logger at runtime, based on another target. Let's assume that I have a functionnal Foo
logger: I'd like to generate separate rules for Foo.1
, Foo.2
, etc etc, but I can't know what will be the last logger I'll have to create (thus, I can't define them declaratively).
So far, I did something like this:
public GetNewClonedLogger(int fooId)
{
var config = NLog.LogManager.Configuration;
var newFooName = $"Foo.{fooId}";
FileTarget baseTarget = NLog.LogManager.Configuration.FindTargetByName<FileTarget>("Foo");
// Copy the base target
var newTarget = new FileTarget(newFooName)
{
Layout = baseTarget.Layout,
CreateDirs = true,
FileName = baseTarget .FileName.ToString().Replace("${var:filename}", newFooName),
ArchiveAboveSize = baseTarget.ArchiveAboveSize,
BufferSize = baseTarget.BufferSize,
ArchiveFileName = baseTarget.ArchiveFileName,
MaxArchiveFiles = baseTarget.MaxArchiveFiles,
ArchiveDateFormat = baseTarget.ArchiveDateFormat,
ArchiveEvery = baseTarget.ArchiveEvery,
FileAttributes = baseTarget.FileAttributes,
KeepFileOpen = baseTarget.KeepFileOpen,
ArchiveNumbering = baseTarget.ArchiveNumbering,
};
// Add configs & flush
NLog.LogManager.Configuration.AddTarget(newTarget);
NLog.LogManager.Configuration.AddRuleForAllLevels(newTarget, newFooName);
// I tried every combinations of the following, without success
NLog.LogManager.ReconfigExistingLoggers();
NLog.LogManager.Configuration = config;
NLog.LogManager.Configuration.Reload();
NLog.LogManager.GetLogger(newFooName);
}
This function returns a logger, that seems OK on every points: the target name is OK, the filename is OK, the NLog.LogManager.Configuration
has a new rule with the correct name (Foo.1
, Foo.2
, ...), and so are AllTargets
. But when I log using the returned logger, no file is created, and it is exacly like nothing happened at all....
I've tried the solution proposed in this thread, without success.... What am I missing?
Thanks for your help!