在以编程方式设置XmlLoggingConfiguration时,NLog不记录任何内容

时间:2018-03-06 20:30:53

标签: c# nlog

这适用于C#.NET的NLog版本4.0.1。

所以我认为这是一个相当独特的问题,我无法使用NLog的标准配置文件位置来加载配置xml文件。在我的情况下唯一可行的解​​决方案是使用一个包含NLog.config(xml配置文件)位置的注册表项,并且在加载NLog时,必须读取注册表项,以便NLog知道在哪里可以找到配置文件。从我在下面生成的TRACE级别内部日志文件中可以看到,此配置加载效果很好。问题是,当我运行程序并尝试进行任何类型的日志记录时,除了内部日志记录之外,不会发生实际的日志记录。

我注意到在内部日志级别没有记录任何错误,并且我的程序中没有异常,所以那里没有明显的问题。我唯一能想到的是NLog不喜欢这样手动设置的配置吗?任何帮助或解决方案将不胜感激。

下面是基类的片段,其中定义了记录器,并提供了如何使用它的快速示例。

public class MyBaseClass
{
    private ILog _Logger = null;
    public ILog Logger
    {
        get
        {
            if(_Logger == null)
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\SomeRegistryLocation\\Configuration", false);
                NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration((string)rk.GetValue("NLogConfigFile"), true);
                _Logger = LogManager.GetLogger(typeof(MyBaseClass));
            }
            return _Logger;
        }
    }

    public void SomeMethod()
    {
        Logger.Info("Hello, is this working?");
    }
}

这是NLog.config xml。

<?xml version="1.0" encoding="utf-8" ?>
<nlog internalLogFile="C:\NLog\NLog_Internal.txt" 
      internalLogLevel="trace"
      internalLogToConsole="true"
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true">
  <extensions>
    <add assembly="Something.NLogExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
    <!-- query-string layout renderer can be found here -->
  </extensions>

  <targets>
      <target xsi:type="File" name="logfile" fileName="C:\NLog\Test_Log.txt" layout="${longdate} ${logger} [${level}] - ${message}"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile"/>
  </rules>
</nlog>

这是我尝试运行程序时NLog内部日志记录的输出。请注意,它不包含任何错误。

2018-03-06 14:46:08.7653 Info Loading assembly name: Something.NLogExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf
2018-03-06 14:46:08.7734 Debug ScanAssembly('Something.NLogExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf')
2018-03-06 14:46:08.7995 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.8094 Trace Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2018-03-06 14:46:08.8196 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2018-03-06 14:46:08.8196 Trace Wrapping LevelLayoutRenderer with UppercaseLayoutRendererWrapper
2018-03-06 14:46:08.8326 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.8326 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:08.8326 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.8473 Trace Scanning UppercaseLayoutRendererWrapper 'Layout Renderer: ${uppercase}'
2018-03-06 14:46:08.8473 Trace  Scanning SimpleLayout ''''
2018-03-06 14:46:08.8473 Trace   Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:08.8473 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.8637 Trace Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2018-03-06 14:46:08.8637 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.8637 Trace Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2018-03-06 14:46:08.8825 Debug Setting 'FileTarget.name' to 'logfile'
2018-03-06 14:46:08.8825 Debug Setting 'FileTarget.fileName' to 'C:\NLog\Test_Log.txt'
2018-03-06 14:46:08.8946 Debug Setting 'FileTarget.layout' to '${longdate} ${logger} [${level}] - ${message}'
2018-03-06 14:46:08.8946 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.8946 Trace Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2018-03-06 14:46:08.8946 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.9123 Trace Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2018-03-06 14:46:08.9123 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.9123 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:08.9123 Trace FindReachableObject<NLog.Internal.IRenderable>:
2018-03-06 14:46:08.9274 Trace Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2018-03-06 14:46:08.9274 Info Adding target File Target[logfile]
2018-03-06 14:46:08.9274 Debug Registering target logfile: NLog.Targets.FileTarget
2018-03-06 14:46:08.9444 Trace ParseRulesElement
2018-03-06 14:46:08.9444 Info Configured from an XML element in C:\Program Files\Common Files\NLog\NLog.config...
2018-03-06 14:46:08.9615 Debug --- NLog configuration dump. ---
2018-03-06 14:46:08.9615 Debug Targets:
2018-03-06 14:46:08.9615 Info File Target[logfile]
2018-03-06 14:46:08.9754 Debug Rules:
2018-03-06 14:46:08.9754 Info logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ logfile ]
2018-03-06 14:46:08.9754 Debug --- End of NLog configuration dump ---
2018-03-06 14:46:08.9915 Trace FindReachableObject<System.Object>:
2018-03-06 14:46:08.9915 Trace Scanning LoggingRule 'logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ logfile ]'
2018-03-06 14:46:08.9915 Trace  Scanning FileTarget 'File Target[logfile]'
2018-03-06 14:46:09.0064 Trace   Scanning SimpleLayout ''C:\NLog\Test_Log.txt''
2018-03-06 14:46:09.0064 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.0218 Trace   Scanning SimpleLayout ''${longdate} ${logger} [${level}] - ${message}''
2018-03-06 14:46:09.0218 Trace    Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2018-03-06 14:46:09.0396 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.0396 Trace    Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2018-03-06 14:46:09.0396 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.0553 Trace    Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:09.0553 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.0553 Trace    Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2018-03-06 14:46:09.0734 Info Found 12 configuration items
2018-03-06 14:46:09.0848 Trace Initializing Layout Renderer: ${message}
2018-03-06 14:46:09.0848 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.0848 Trace Initializing Layout Renderer: ${level}
2018-03-06 14:46:09.1028 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.1028 Trace Initializing Layout Renderer: ${logger}
2018-03-06 14:46:09.1136 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.1136 Trace Initializing Layout Renderer: ${longdate}
2018-03-06 14:46:09.1136 Trace Initializing '${longdate} ${logger} [${level}] - ${message}'
2018-03-06 14:46:09.1136 Trace FindReachableObject<System.Object>:
2018-03-06 14:46:09.1356 Trace Scanning SimpleLayout ''${longdate} ${logger} [${level}] - ${message}''
2018-03-06 14:46:09.1356 Trace  Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2018-03-06 14:46:09.1503 Trace  Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.1503 Trace  Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2018-03-06 14:46:09.1503 Trace  Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.1614 Trace  Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:09.1614 Trace  Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.1614 Trace  Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2018-03-06 14:46:09.1846 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.1846 Trace Initializing 'C:\NLog\Test_Log.txt'
2018-03-06 14:46:09.1944 Trace FindReachableObject<System.Object>:
2018-03-06 14:46:09.1944 Trace Scanning SimpleLayout ''C:\NLog\Test_Log.txt''
2018-03-06 14:46:09.1944 Trace  Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.2087 Trace Initializing File Target[logfile]
2018-03-06 14:46:09.2087 Trace FindReachableObject<NLog.Layouts.Layout>:
2018-03-06 14:46:09.2313 Trace Scanning FileTarget 'File Target[logfile]'
2018-03-06 14:46:09.2415 Trace  Scanning SimpleLayout ''C:\NLog\Test_Log.txt''
2018-03-06 14:46:09.2415 Trace   Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.2415 Trace  Scanning SimpleLayout ''${longdate} ${logger} [${level}] - ${message}''
2018-03-06 14:46:09.2553 Trace   Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2018-03-06 14:46:09.2553 Trace   Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.2553 Trace   Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2018-03-06 14:46:09.2696 Trace   Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.2696 Trace   Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:09.2696 Trace   Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.2863 Trace   Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2018-03-06 14:46:09.2863 Trace File Target[logfile] has 2 layouts
2018-03-06 14:46:09.2863 Trace FindReachableObject<System.Object>:
2018-03-06 14:46:09.3024 Trace Scanning LoggingRule 'logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ logfile ]'
2018-03-06 14:46:09.3024 Trace  Scanning FileTarget 'File Target[logfile]'
2018-03-06 14:46:09.3024 Trace   Scanning SimpleLayout ''C:\NLog\Test_Log.txt''
2018-03-06 14:46:09.3213 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.3213 Trace   Scanning SimpleLayout ''${longdate} ${logger} [${level}] - ${message}''
2018-03-06 14:46:09.3334 Trace    Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2018-03-06 14:46:09.3334 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.3334 Trace    Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2018-03-06 14:46:09.3563 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.3563 Trace    Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2018-03-06 14:46:09.3694 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-03-06 14:46:09.3694 Trace    Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2018-03-06 14:46:09.3833 Info Found 12 configuration items
2018-03-06 14:46:09.3949 Trace Initializing Layout Renderer: ${message}
2018-03-06 14:46:09.3949 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.3949 Trace Initializing Layout Renderer: ${level}
2018-03-06 14:46:09.4195 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.4364 Trace Initializing Layout Renderer: ${logger}
2018-03-06 14:46:09.4476 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.4574 Trace Initializing Layout Renderer: ${longdate}
2018-03-06 14:46:09.4574 Trace Initializing '${longdate} ${logger} [${level}] - ${message}'
2018-03-06 14:46:09.4574 Trace Initializing Layout Renderer: ${literal}
2018-03-06 14:46:09.4735 Trace Initializing 'C:\NLog\Test_Log.txt'
2018-03-06 14:46:09.4735 Trace Initializing File Target[logfile]
2018-03-06 14:46:09.4735 Info Watching path 'C:\Program Files\Common Files\NLog' filter 'NLog.config' for changes.
2018-03-06 14:46:09.7739 Info Shutting down logging...
2018-03-06 14:46:09.7739 Info Stopping file watching for path 'C:\Program Files\Common Files\NLog' filter 'NLog.config'
2018-03-06 14:46:09.7739 Info Closing old configuration.
2018-03-06 14:46:09.7895 Trace LogFactory.Flush(00:00:15)
2018-03-06 14:46:09.7895 Trace Flushing all targets...
2018-03-06 14:46:09.8014 Trace ForEachItemInParallel() 1 items
2018-03-06 14:46:09.8014 Trace Continuation invoked: 
2018-03-06 14:46:09.8014 Trace Parallel task completed. 0 items remaining
2018-03-06 14:46:09.8247 Debug Closing logging configuration...
2018-03-06 14:46:09.8247 Trace Closing File Target[logfile]
2018-03-06 14:46:09.8395 Trace Closing 'C:\NLog\Test_Log.txt'
2018-03-06 14:46:09.8395 Trace Closing Layout Renderer: ${literal}
2018-03-06 14:46:09.8489 Trace Closing '${longdate} ${logger} [${level}] - ${message}'
2018-03-06 14:46:09.8489 Trace Closing Layout Renderer: ${longdate}
2018-03-06 14:46:09.8489 Trace Closing Layout Renderer: ${literal}
2018-03-06 14:46:09.8489 Trace Closing Layout Renderer: ${logger}
2018-03-06 14:46:09.8656 Trace Closing Layout Renderer: ${literal}
2018-03-06 14:46:09.8656 Trace Closing Layout Renderer: ${level}
2018-03-06 14:46:09.8656 Trace Closing Layout Renderer: ${literal}
2018-03-06 14:46:09.8824 Trace Closing Layout Renderer: ${message}
2018-03-06 14:46:09.8824 Debug Finished closing logging configuration.
2018-03-06 14:46:09.8824 Info Logger has been shut down.

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。不幸的是,更新NLog不是解决方案所以经过更多的阅读和调查我意识到我已经正确配置了NLog,但是Common.Logging根本没有配置。因此,当我使用ILog接口记录Info语句时,由于没有正确配置Common.Logging,所以不会发生任何事情。