I have referred this Update NLog target filename at runtime and many other links but none seems to be working in my case. I initialize logger using
private Logger logger = LogManager.GetCurrentClassLogger();
All the log messages are queued, then emptied using a timer.
this.messageQueue = new ConcurrentQueue<NotificationMessagePacket>();
this.timer = new System.Timers.Timer(1000);
this.timer.Elapsed += (o, e) =>
{
if (!isWriting)
{
lock (_lockObject)
{
isWriting = true;
NotificationMessagePacket packet;
while (this.messageQueue.TryDequeue(out packet) && packet != null)
{
try
{
if (!string.IsNullOrWhiteSpace(packet.DetailMessage))
this.Infolog(packet);
}
catch (Exception ex)
{
ObjectUtils.EventLogWriteError("NotificationMessagePacket emptying error : " + ex.ToString(),
EventLogEntryType.Warning);
}
}
}
isWriting = false;
}
};
this.timer.Start();
I am try to reset the filename of my target using the following code:
LoggingConfiguration configuration = LogManager.Configuration;
var wrapper = (AsyncTargetWrapper)configuration.FindTargetByName("log");
var target = (FileTarget)wrapper.WrappedTarget;
string path = Path.Combine(basePath, "Test", "Log", string.Concat(DateTime.Now.ToString("dd"), "_", DateTime.Now.ToString("MMMM"), "_", DateTime.Now.Year + @"\AppLogs.txt"));
if (!string.IsNullOrEmpty(message.ProcessId))
path = Path.Combine(basePath, "Test", "Log", string.Concat(DateTime.Now.ToString("dd"), "_", DateTime.Now.ToString("MMMM"), "_", DateTime.Now.Year + @"\" + message.ProcessId + ".txt"));
target.FileName = path;
target.ConcurrentWrites = false;
LogManager.Configuration = configuration;
LogManager.ReconfigExistingLoggers();
But sometimes, log message aren't written to appropriate file.