How to get the filename of a filetarget?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the filename of a filetarget?

Carl Thomsen
Hi folks,

I'm going to develop a logfile viewer for one of our applications.
I tried the code (from http://nlog-forum.1685105.n2.nabble.com/How-to-get-FileTarget-s-real-filename-for-log-viewing-td1685873.html)
/***********************/
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
}
/***********************/

But i have 2 problems.
1. LoggingConfiguration config = LogManager.Configuration;
     This is NULL in runtime !!!
2. string expandedFileName = NLog.Layout.Evaluate(standardTarget.FileName);
    In NLog 2.0:
        Layout type has been moved to NLog.Layouts namespace and renamed SimpleLayout.
    and
      FileTarget type has updated properties
            Property FileName has different type: Layout (was String)

Can anyone help me?

Carl
Reply | Threaded
Open this post in threaded view
|

Re: How to get the filename of a filetarget?

Carl Thomsen
I've found the answers.

1. the nlog.config was not compiled to output directory

2. the code has to be changed:

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

if (standardTarget != null)
{
     string expandedFileName = NLog.Layouts.SimpleLayout.Evaluate(standardTarget.FileName.ToString());

 
     // do what you want
}
/***********************/