How to get path of current target file using NLog in runtime?
I use NLog with next configuration:
<targets>
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="f" />
</rules>
I tried to get FileName
property of FileTarget
(I check, that there only one FileTarget in collection)
NLog.LogManager.GetCurrentClassLogger().Info("test");
var logFile = (from t in NLog.LogManager.Configuration.AllTargets
where t is NLog.Targets.FileTarget
select (NLog.Targets.FileTarget)t).FirstOrDefault();
But logFile.FileName contains only pattern of file name, exactly how it's specified in settings.
How can I get in runtime path of current log file?
This did the trick for me:
var fileTarget = (FileTarget) LogManager.Configuration.FindTargetByName("file");
// Need to set timestamp here if filename uses date.
// For example - filename="${basedir}/logs/${shortdate}/trace.log"
var logEventInfo = new LogEventInfo {TimeStamp = DateTime.Now};
string fileName = fileTarget.FileName.Render(logEventInfo);
if (!File.Exists(fileName))
throw new Exception("Log file does not exist.");
This method will work even if you have set async="true"
(i.e. your target is wrapped by an AsyncTargetWrapper
) in your NLog XML configuration:
private string GetLogFileName(string targetName)
{
string fileName = null;
if (LogManager.Configuration != null && LogManager.Configuration.ConfiguredNamedTargets.Count != 0)
{
Target target = LogManager.Configuration.FindTargetByName(targetName);
if (target == null)
{
throw new Exception("Could not find target named: " + targetName);
}
FileTarget fileTarget = null;
WrapperTargetBase wrapperTarget = target as WrapperTargetBase;
// Unwrap the target if necessary.
if (wrapperTarget == null)
{
fileTarget = target as FileTarget;
}
else
{
fileTarget = wrapperTarget.WrappedTarget as FileTarget;
}
if (fileTarget == null)
{
throw new Exception("Could not get a FileTarget from " + target.GetType());
}
var logEventInfo = new LogEventInfo { TimeStamp = DateTime.Now };
fileName = fileTarget.FileName.Render(logEventInfo);
}
else
{
throw new Exception("LogManager contains no Configuration or there are no named targets");
}
if (!File.Exists(fileName))
{
throw new Exception("File " + fileName + " does not exist");
}
return fileName;
}
Targets can be wrapped multiple times (in my case I had a filter), so the following snippet is a more generic approach to unwrapping that works for multiple levels and doesn't make assumptions about target names.
Target target = LogManager.Configuration.FindTargetByName(targetName);
while ((target != null) && (target is WrapperTargetBase))
{
target = (target as WrapperTargetBase).WrappedTarget;
}