How to get path of current target file using NLog in runtime?

前端 未结 4 1712
猫巷女王i
猫巷女王i 2021-01-31 07:36

I use NLog with next configuration:

  
    

        
相关标签:
4条回答
  • 2021-01-31 07:57

    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.");
    
    0 讨论(0)
  • 2021-01-31 08:01

    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
    
    0 讨论(0)
  • 2021-01-31 08:08

    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;
        }
    
    0 讨论(0)
  • 2021-01-31 08:17

    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;
    }
    
    0 讨论(0)
提交回复
热议问题