如何用新的 Logger 实例替换 NLog Logger?

How to replace NLog Logger with a new instance of Logger?

所以,刚开始使用NLog。我正在执行一个编程实现,我正在尝试设置一个可以导入到任何项目中的 class。 class 有两个方法:CreateLogger() 和 GenerateLog()。这是 class 的完整内容:

using System;
using NLog;
using NLog.Config;
using NLog.Targets;

namespace LogEngine
{
    /// <summary>
    ///     Create an instance of NLog for use on a per class level. 
    /// </summary>
    internal sealed class EventLog
    {
        #region Internal Methods

        /// <summary>
        ///     Generates the NLog.Logger object that will control logging facilities in this program. 
        /// </summary>
        /// <returns>
        ///     static reference to a <see cref="NLog.Logger" /> object. 
        /// </returns>
        internal static Logger CreateLogger(string baseDir = @"${basedir}\")
        {
            // Setup log configuration object and new file and screen output targets.
            var config = new LoggingConfiguration();

            var screenTarget = new ConsoleTarget();
            config.AddTarget("screen", screenTarget);

            var fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            screenTarget.Layout = @"${newline}${message}";

            var MinScreenOutput = new LoggingRule("*", LogLevel.Fatal, screenTarget);
            config.LoggingRules.Add(MinScreenOutput);

            // Set the properties for the file output target.
            fileTarget.FileName = baseDir + @"${appdomain:format={1\}} logs${shortdate}.log";
            fileTarget.Layout = @"${longdate} ${pad:padcharacter=~:padding=29:inner= ${level:uppercase=true}}"
                              + @" ${pad:padcharacter=~:padding=30:inner= Event ID\: ${event-properties:item=EventCode}}"
                              + @"${newline}${message} ${when:when=level == 'Error':inner=${newline}Class / Method\:"
                              + @"${pad:padding=9:inner=}${callsite:fileName=true:includeSourcePath=false:skipFrames=1}"
                              + @"${newline}Exception\:${pad:padding=14:inner=}${exception}}${newline}";

            // Define what sort of events to send to the file output target.
            var MinOutputDebug = new LoggingRule("*", LogLevel.Debug, fileTarget);
            config.LoggingRules.Add(MinOutputDebug);

            // Set the configuration for the LogManager
            LogManager.Configuration = config;

            // Get the working instance of the logger.
            return LogManager.GetLogger("LogEngine");
        }

        /// <summary>
        ///     Passes one log entry to the destination logger. 
        /// </summary>
        /// <param name="log">
        ///     The <see cref="NLog.Logger" /> object to write the log entry to. 
        /// </param>
        /// <param name="eventId">
        ///     Four character unique event ID as <see cref="System.String" />. 
        /// </param>
        /// <param name="level">
        ///     The <see cref="NLog.LogLevel" /> value. 
        /// </param>
        /// <param name="message">
        ///     The message to save to the log file as <see cref="System.String" />. 
        /// </param>
        /// <param name="ex">
        ///     If this is an error log event, pass it as an <see cref="System.Exception" /> object. 
        /// </param>
        internal static void GenerateLog(Logger log, string eventId, LogLevel level, string message, Exception ex = null)
        {
            // Values used for all events.
            LogEventInfo logEvent = new LogEventInfo();
            logEvent.Properties["EventCode"] = eventId;
            logEvent.Level = level;
            logEvent.Message = message;

            // If we have an error log, make sure the exception is passed.
            if (level.Equals(LogLevel.Error))
                logEvent.Exception = ex;

            // Actually write the log entry.
            log.Log(logEvent);

            if (level.Equals(LogLevel.Error) || level.Equals(LogLevel.Fatal))
                System.Environment.Exit(Convert.ToInt32(eventId));
        }

        #endregion Internal Methods
    }
}

在 CreateLogger() 方法中,您会看到有一个默认参数。因此,当我在 class 开始时在我的程序中调用 CreateLogger() 时,我没有传递任何参数,并且 ${basedir} 值用于生成初始日志记录:

internal class Program
{
    #region Private Fields

    private static Logger log = EventLog.CreateLogger();

    #endregion Private Fields
...

但是,在执行期间,我需要将日志记录位置从 ${basedir} 更改为我从 SQL 数据库中提取的值。这是我的做法:

if (!path.Equals(null))
{
    sArgs.path = path.ToString().Trim();
    //NLog.Config.SimpleConfigurator.ConfigureForFileLogging(sArgs.path + @"Logs\log1.txt", LogLevel.Debug);
    //LogManager.Shutdown();
    //LogManager.ReconfigExistingLoggers();
    log = EventLog.CreateLogger(sArgs.path);
    LogManager.ReconfigExistingLoggers();
}

"path" 是调用 SQLCommand.ExecuteScalar() 返回的对象。它是我需要将 Logger 连接到的 ${basedir} 的替代品。如果路径不为空,则我将其转换为字符串并将其存储到实例化为 "sArgs" 的单例 class 中。这里有一些注释掉的代码来展示我是如何尝试解决这个问题的。

好的,所以我在最后一个代码块中看到了(当我将 "log" 设置为由 CreateLogger(sArgs.path) 生成的新实例时)我可以看到我的日志记录路径在日志对象中实际上正在更新。但是,当我第一次有机会记录事件时,它仍在使用旧的 Logger 实例(因此 ${basedir} 仍在使用中,而不是 sArgs.path)。

我的问题是,我遗漏了什么是保留对 "log" 的更改,在调试器中步进我的代码时我可以清楚地看到它实际上成为 Logger 对象的位置?还是我做的 EventLog class 完全错了?

感谢您对此问题提供的任何见解。

对于每个使用 private static Logger log = EventLog.CreateLogger(); 的 class,只要您不想在 EventLogclass 中定义默认的 baseDir,就需要更改 baseDir待用。

您没有提供您的单例 class sArgs 的代码,或您想要使用 EventLog 的 classes 的任何其他示例。

使用您的代码,我只将您的 EventLog 更改为 EventLogger,并将默认的 CreateLogger 更改为 internal static Logger CreateLogger(string baseDir = @"C:\Temp\NLog\Default\"):

using System;
using NLog;

namespace ConsoleApplication1
{
    class Program
    {
        private static Logger log = EventLogger.CreateLogger();

        static void Main(string[] args)
        {

            EventLogger.GenerateLog(log, "1", LogLevel.Debug, "Default", null);

            log = EventLogger.CreateLogger(@"C:\Temp\NLog\New\");
            LogManager.ReconfigExistingLoggers();

            EventLogger.GenerateLog(log, "2", LogLevel.Debug, "New", null);

            Class1.DoSomething();

            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
         }
    }
}

和第 1 类:

using NLog;

namespace ConsoleApplication1
{
    public static class Class1
    {
        private static Logger log = EventLogger.CreateLogger();

        public static void DoSomething()
        {
            EventLogger.GenerateLog(log, "3", LogLevel.Debug, "Class1.DoSomething", null);
        }
    }
}

运行 代码产生以下输出:

日志 EventId 1 将被写入 C:\Temp\NLog\Default\ConsoleApplication1.vshost.exe\logs 日志 EventId 2 将写入 C:\Temp\NLog\New\ConsoleApplication1.vshost.exe\logs

和Class1.cs中的LogEventId 3会被写入C:\Temp\NLog\Default\ConsoleApplication1.vshost.exe\logs中,因为在Class1.cs中初始化日志时,使用了默认路径。如果您想在 Class1.cs(以及随后的 classes)中更改日志的 baseDir,您将需要单独更新路径。

希望这对您有所帮助。

谢谢@Riann 的帮助和评论。实际上,我在不影响 ${callsite} 了解捕获错误的实际方法和行的能力的情况下,将其作为单例 class 进行了工作。这是我从更新的 EventLog class:

开始的做法
using System;
using NLog;
using NLog.Config;
using NLog.Targets;

namespace LogEngine
{
    /// <summary>
    ///     Create an instance of NLog for use on a per class level. 
    /// </summary>
    internal sealed class EventLog
    {
        #region Constructors

        static EventLog()
        {
        }

        private EventLog()
        {
            this.CreateLogger();
        }

        #endregion Constructors

        #region Singleton Objects

        internal static EventLog logger { get { return _logger; } }

        private static readonly EventLog _logger = new EventLog();

        #endregion Singleton Objects

        #region Private Fields

        private static Logger _log;

        #endregion Private Fields

        #region Internal Methods

        /// <summary>
        ///     Generates the NLog.Logger object that will control logging facilities in this program. 
        /// </summary>
        internal void CreateLogger(string baseDir = @"${basedir}\")
        {
            // Setup log configuration object and new file and screen output targets.
            var config = new LoggingConfiguration();

            var screenTarget = new ConsoleTarget();
            config.AddTarget("screen", screenTarget);

            var fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            screenTarget.Layout = @"${newline}${message}";

            var MinScreenOutput = new LoggingRule("*", LogLevel.Fatal, screenTarget);
            config.LoggingRules.Add(MinScreenOutput);

            // Set the properties for the file output target.
            fileTarget.FileName = baseDir + @"${appdomain:format={1\}} logs${shortdate}.log";
            fileTarget.Layout = @"${longdate} ${pad:padcharacter=~:padding=29:inner= ${level:uppercase=true}}"
                                + @" ${pad:padcharacter=~:padding=30:inner= Event ID\: ${event-properties:item=EventCode}}"
                                + @"${newline}${message} ${when:when=level == 'Error':inner=${newline}Class / Method\:"
                                + @"${pad:padding=9:inner=}${callsite:fileName=true:includeSourcePath=false:skipFrames=1}"
                                + @"${newline}Exception\:${pad:padding=14:inner=}${exception}}"
                                + @"${when:when=level == 'Fatal':inner=${newline}Class / Method\:"
                                + @"${pad:padding=9:inner=}${callsite:fileName=true:includeSourcePath=false:skipFrames=1}"
                                + @"${newline}Exception\:${pad:padding=14:inner=}${exception}}${newline}";

            // Define what sort of events to send to the file output target.
            var MinOutputDebug = new LoggingRule("*", LogLevel.Debug, fileTarget);
            config.LoggingRules.Add(MinOutputDebug);

            // Set the configuration for the LogManager
            LogManager.Configuration = config;

            // Get the working instance of the logger.
            _log = LogManager.GetLogger("LogEngine");
        }

        /// <summary>
        ///     Passes one log entry to the destination logger and associated exception information. 
        /// </summary>
        /// <remarks>
        ///     Use this form of the method when <see cref="NLog.LogLevel.Error" /> or
        ///     <see cref="NLog.LogLevel.Fatal" /> is used.
        /// </remarks>
        /// <param name="caller">
        ///     <see cref="System.String" /> holding information about the calling method. 
        /// </param>
        /// <param name="eventId">
        ///     Four character unique event ID as <see cref="System.String" />. 
        /// </param>
        /// <param name="level">
        ///     The <see cref="NLog.LogLevel" /> value. 
        /// </param>
        /// <param name="message">
        ///     The message to save to the log file as <see cref="System.String" />. 
        /// </param>
        /// <param name="ex">
        ///     If this is an error log event, pass it as an <see cref="System.Exception" /> object. 
        /// </param>
        internal void GenerateLog(string eventId, LogLevel level, string message, Exception ex)
        {
            // Values used for all events.
            LogEventInfo logEvent = new LogEventInfo();
            logEvent.Properties["EventCode"] = eventId;
            logEvent.Level = level;
            logEvent.Message = message;

            logEvent.Exception = ex;

            // Actually write the log entry.
            _log.Log(logEvent);

            if (level.Equals(LogLevel.Error) || level.Equals(LogLevel.Fatal))
                Environment.Exit(Convert.ToInt32(eventId));
        }

        /// <summary>
        ///     Passes one log entry to the destination logger. 
        /// </summary>
        /// <remarks>
        ///     Use this form of the method when <see cref="NLog.LogLevel.Warn" /> or
        ///     <see cref="NLog.LogLevel.Info" /> is used.
        /// </remarks>
        /// <param name="caller">
        ///     <see cref="System.String" /> holding information about the calling method. 
        /// </param>
        /// <param name="eventId">
        ///     Four character unique event ID as <see cref="System.String" />. 
        /// </param>
        /// <param name="level">
        ///     The <see cref="NLog.LogLevel" /> value. 
        /// </param>
        /// <param name="message">
        ///     The message to save to the log file as <see cref="System.String" />. 
        /// </param>
        internal void GenerateLog(string eventId, LogLevel level, string message)
        {
            // Values used for all events.
            LogEventInfo logEvent = new LogEventInfo();
            logEvent.Properties["EventCode"] = eventId;
            logEvent.Level = level;
            logEvent.Message = message;

            // Actually write the log entry.
            _log.Log(logEvent);
        }

        #endregion Internal Methods
    }
}

基本上,除了 Contructor、Singleton Objects 和 Private Fields 区域之外,所有这些都是新的,我必须更改 CreateLogger() 和 GenerateLog() 中的调用以影响私有字段 _log。我也确实使 GenerateLogger() 成为一个重载方法,但这并不影响 EventLog 的整体使用 class.

在我的其他每个 classes 中,我只需要更改打开记录器的方式。我打算在方法级别执行此操作,但决定在 class 级别执行此操作:

internal class Program
{
    #region Private Fields

    private static readonly EventLog log = EventLog.logger;

    #endregion Private Fields
...

因此,对于我使用的任何方法,如果我想更改我的日志记录路径,我只需要让我的日志实例调用 CreateLogger(path):

if (!path.Equals(null))
{
    sArgs.path = path.ToString().Trim();
    log.CreateLogger(sArgs.path);
}

任何未来的调用,无论我在什么地方 class,我只需要调用 GenerateLog(),我就会有正确的日志路径。