Code Coverage by Clover.NET

Re: How to get FileTarget's real filename for log viewing

by Lars2b :: Rate this Message:

Reply to Author | View in Thread

Hi,

it's been a long time since the question was posted, but I stumbled across it when I was facing exactly the same problem.

I did not find a way to get the evaluated filename directly either, but I had no problems using this code, which needs less string replacements:

/***********************/
LoggingConfiguration config = LogManager.Configuration;
FileTarget standardTarget = config.FindTargetByName("YourFileTargetName") as FileTarget;

if (standardTarget != null)
{
     string expandedFileName = NLog.Layout.Evaluate(standardTarget.FileName);

     // paths like c:\test\/myLogfiles/Log.txt are handled correctly
     string directoryPath = Path.GetDirectoryName(expandedFileName);

     if (!Directory.Exists(directoryPath))
     {
           // error handling
     }
     // do what you want
}
/***********************/

Note that I needed the directory and not the log file name as in the previous post (use Path.GetFileName() for that case).

Have a nice day
Lars

LightInTheBox - Buy quality products at wholesale price