I want to have a log file viewer for my file target log file in my application. My configuration is very simple - just one log file. However, the code I have come up with to get the filename is really complex. Here it is:
TargetCollection targets = logger.Factory.Configuration.GetConfiguredNamedTargets();
foreach (Target target in targets)
{
if (target is FileTarget)
{
string baseName = ((FileTarget)target).FileName;
// expand the variables in the name, like ${basedir}
string realName = NLog.Layout.Evaluate(baseName);
// unfortunately, this can end up with something like c:\baseDirectory\/log/YYYY-MM-DD.txt,
// need to clean up the filename for display purposes
realName = realName.Replace('/', Path.DirectorySeparatorChar);
realName = realName.Replace("" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar,
"" + Path.DirectorySeparatorChar);
FileInfo fileInfo = new FileInfo(realName);
if (fileInfo.Exists)
{
// code here to display the log file in the file realName in my viewing Form
}
else
{
MsgBox.Show("There is no log file for today");
}
return;
}
}
MsgBox.Show("There is no configured file logger");
Is there any simpler way to do this?
Thanks.
Re: How to get FileTarget's real filename for log viewing