66

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?

Julian
  • 33,915
  • 22
  • 119
  • 174
Win4ster
  • 896
  • 1
  • 8
  • 15
  • Not mainly about your question but didn't know you can check Target type by writing: `where t is NLog.Targets.FileTarget`. My code is: `ruleModel.Targets.AddRange( rule.Targets.Where(t => t is NLog.Targets.DatabaseTarget) .Select(t => new LogModels.LoggingTargetModel() { Name = t.Name, Type = LoggingTargetType.DatabaseTarget }) .ToList());` – broadband Mar 10 '16 at 14:25

6 Answers6

81

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.");
Michael
  • 1,743
  • 16
  • 12
  • For me `fileTarget.FileName` is a string and I `cannot resolve symbol 'Render'` any ideas? – tpower May 05 '15 at 09:50
  • 1
    @tpower FileName isn't of type string. Its type is NLog.Layouts.Layout. – broadband Mar 17 '16 at 12:04
  • also try `LogEventInfo logEventInfo = new LogEventInfo(); fileTarget.FileName.Render(logEventInfo);` , seems it the default `new LogEventInfo()` will auto bring and fill the log file info, even for very rare use NLog Parameter. – yu yang Jian Jun 01 '21 at 07:21
37

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;
    }
markmuetz
  • 9,334
  • 2
  • 32
  • 33
4

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;
}
Skip
  • 121
  • 1
  • 4
3

Here's some somewhat simplified implementations that work for me --for newer versions of .Net/NLog at least.

If you only have one FileTarget and you don't have any async wrapping around it and just want a simple file name:

var file = LogManager.Configuration?.AllTargets.OfType<FileTarget>()
    .Select(x => x.FileName.Render(LogEventInfo.CreateNullEvent()))
    .FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
Console.WriteLine($"Logging to file: {file}");

Output:

Logging to file: C:\MyApp\logs\2022-05-04.log

Alternatively, here's a method that unwraps async and buffered targets too:

public static IEnumerable<string> GetNLogFileTargets()
{
    var allTargets = LogManager.Configuration?.AllTargets;
    if (allTargets == null)
    {
        return Enumerable.Empty<string>();
    }

    var wrappedFileTargets = allTargets.OfType<WrapperTargetBase>()
        .Where(x => x.WrappedTarget is FileTarget)
        .Select(x => x.WrappedTarget).Cast<FileTarget>();
    var fileTargets = allTargets.OfType<FileTarget>().Concat(wrappedFileTargets);
    return fileTargets.Select(x => x.FileName.Render(LogEventInfo.CreateNullEvent())).Where(x => !string.IsNullOrWhiteSpace(x));
}
Jon
  • 9,156
  • 9
  • 56
  • 73
2

I know that my answer is not exactly answering the question, but the most difficult thing is to find the right target and cast it properly, then we can access any properties. I also didn't find a question that would fit my answer thus posting here...

This method will work even if you have set async="true" (i.e. your target is wrapped by an AsyncTargetWrapper or any TargetWrapper) in your NLog XML configuration:

Using NLog Version: 3.1.0.0

Runtime Version: v4.0.30319

    private Target FindTargetByName(string targetName)
    {
        if (LogManager.Configuration == null)
            return null;

        Target t = LogManager.Configuration.FindTargetByName(targetName);

        if (t is NLog.Targets.Wrappers.WrapperTargetBase)
        {
            var list = LogManager.Configuration.AllTargets.ToList();
            t = list.Find(x => x.Name == targetName + "_wrapped");
            return t;
        }
        else
        {
            return t;
        }
    }

Usage for MailTarget named emailError

var emailError = (MailTarget)FindTargetByName("emailError");
emailError.SmtpServer = "" //you can set or get
Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29
0

This returns the path to the current log file of the "test" Target. If there is no file yet, an NUllReferenceException will be thrown.

  /// <exception cref="NullReferenceException"></exception>
        public static string GetCurrentDebugLogFilePath()
        {
            return (LogManager.Configuration.FindTargetByName("test") as FileTarget).FileName.Render(new LogEventInfo { TimeStamp = DateTime.Now });
        }
Michael Santos
  • 466
  • 7
  • 15