Code Coverage by Clover.NET

How to get FileTarget's real filename for log viewing

View: New views
2 Messages — Rating Filter:   Alert me  

How to get FileTarget's real filename for log viewing

by Dan Cooperstock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by Lars2b :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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