【问题标题】:NLog not writing to file after explict reload of configuration显式重新加载配置后,NLog 未写入文件
【发布时间】:2021-07-01 11:16:59
【问题描述】:

这基本上是this 的副本,我知道,但即使在关注其他帖子和故障排除指南之后,我仍然卡住了。

它是一个使用 NLog 4.7.10 的 .net 标准 2.0 库,没有配置文件,执行以下操作:

internal Log()
{
    NLog.Common.InternalLogger.LogLevel = LogLevel.Trace;
    NLog.Common.InternalLogger.LogToConsole = true;
    NLog.Common.InternalLogger.LogFile = "nlog-internal.txt";

    LogManager.Configuration = new NLog.Config.LoggingConfiguration();
    LogManager.ThrowExceptions = true;

    this.Core = this.CustomLog("core");
    this.Core.Info("Done!");
}

public Logger CustomLog(string name)
{
    if (name == null)
        throw new ArgumentNullException(name);

    if (LogManager.Configuration.FindRuleByName(name) == null)
    {
        FileTarget target = new FileTarget(name)
        {
            FileName = string.Format("{0}.txt", name),
            Layout = DefaultLayout,
            AutoFlush = true,
            DeleteOldFileOnStartup = true
        };

        LogManager.Configuration.AddTarget(target);
        LogManager.Configuration.AddRule(LogLevel.Trace, LogLevel.Fatal, target, name);
        LogManager.Configuration.Reload();
    }

    return LogManager.GetLogger(name);
}

然后将所有内容导入 net core 3.1 应用程序

我得到的: nlog-internal.txt 写好了,控制台显示的信息一样

2021-07-01 13:09:28.6727 Debug --- NLog configuration dump ---
2021-07-01 13:09:28.6727 Debug Targets:
2021-07-01 13:09:28.6727 Debug Rules:
2021-07-01 13:09:28.6727 Debug --- End of NLog configuration dump ---
2021-07-01 13:09:28.6924 Trace FindReachableObject<System.Object>:
2021-07-01 13:09:28.6924 Info Validating config: Targets=0, ConfigItems=0
2021-07-01 13:09:28.7186 Debug ScanAssembly('NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2021-07-01 13:09:28.7602 Debug Found assembly location directory: 'D:\Work\netcoreapp3.1' (NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c)
2021-07-01 13:09:28.7602 Debug Auto loading based on NLog-Assembly found location: D:\Work\netcoreapp3.1
2021-07-01 13:09:28.7602 Debug Search for auto loading files in location: D:\Work\netcoreapp3.1
2021-07-01 13:09:28.7694 Debug Found assembly location directory: 'D:\Work\netcoreapp3.1' (BrokenGalaxy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
2021-07-01 13:09:28.7694 Debug Auto loading based on GetEntryAssembly-Assembly found location: D:\Work\netcoreapp3.1
2021-07-01 13:09:28.7694 Debug Auto loading based on AppDomain-BaseDirectory found location: D:\Work\netcoreapp3.1
2021-07-01 13:09:28.7694 Debug Start auto loading, location: D:\Work\netcoreapp3.1
2021-07-01 13:09:28.7694 Debug Auto loading done
2021-07-01 13:09:28.7694 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.7849 Trace Scanning LongDateLayoutRenderer 'Layout Renderer: ${longdate}'
2021-07-01 13:09:28.7849 Debug Setting 'NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper.uppercase' to 'true'
2021-07-01 13:09:28.7849 Trace Wrapping NLog.LayoutRenderers.LevelLayoutRenderer with NLog.LayoutRenderers.Wrappers.UppercaseLayoutRendererWrapper
2021-07-01 13:09:28.7849 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.7849 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2021-07-01 13:09:28.7978 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.7978 Trace Scanning UppercaseLayoutRendererWrapper 'Layout Renderer: ${uppercase}'
2021-07-01 13:09:28.7978 Trace  Scanning Property Inner 'SimpleLayout=Layout Renderer: ${level}' NLog.Layouts
2021-07-01 13:09:28.7978 Trace  Scanning SimpleLayout 'SimpleLayout=Layout Renderer: ${level}'
2021-07-01 13:09:28.7978 Trace   Scanning Property Renderers 'System.Collections.ObjectModel.ReadOnlyCollection`1[NLog.LayoutRenderers.LayoutRenderer]' System.Collections.ObjectModel
2021-07-01 13:09:28.7978 Trace   Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2021-07-01 13:09:28.7978 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.7978 Trace Scanning LoggerNameLayoutRenderer 'Layout Renderer: ${logger}'
2021-07-01 13:09:28.7978 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.7978 Trace Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2021-07-01 13:09:28.8196 Debug Setting 'NLog.LayoutRenderers.DateLayoutRenderer.format' to 'HH:mm:ss.fff'
2021-07-01 13:09:28.8196 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.8196 Trace Scanning DateLayoutRenderer 'Layout Renderer: ${date}'
2021-07-01 13:09:28.8196 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.8196 Trace Scanning LevelLayoutRenderer 'Layout Renderer: ${level}'
2021-07-01 13:09:28.8196 Trace FindReachableObject<NLog.Internal.IRenderable>:
2021-07-01 13:09:28.8196 Trace Scanning MessageLayoutRenderer 'Layout Renderer: ${message}'
2021-07-01 13:09:28.8196 Debug Registered target core: NLog.Targets.FileTarget
2021-07-01 13:09:28.8362 Debug Targets for core by level:
2021-07-01 13:09:28.8362 Debug Trace => core
2021-07-01 13:09:28.8362 Debug Debug => core
2021-07-01 13:09:28.8362 Debug Info => core
2021-07-01 13:09:28.8362 Debug Warn => core
2021-07-01 13:09:28.8362 Debug Error => core
2021-07-01 13:09:28.8362 Debug Fatal => core
2021-07-01 13:09:30.9517 Info AppDomain Shutting down. Logger closing...
2021-07-01 13:09:30.9592 Trace Flushing all 1 targets...
2021-07-01 13:09:30.9592 Trace ForEachItemInParallel() 1 items
2021-07-01 13:09:30.9592 Trace Continuation invoked: 
2021-07-01 13:09:30.9592 Trace Parallel task completed. 0 items remaining
2021-07-01 13:09:30.9694 Debug Flush completed
2021-07-01 13:09:30.9694 Debug Targets not configured for logger: core
2021-07-01 13:09:30.9694 Debug Closing logging configuration...
2021-07-01 13:09:30.9694 Debug Finished closing logging configuration.
2021-07-01 13:09:30.9694 Info Logger has been shut down.

我的期望:还有一个带有“完成!”的 core.txt 文件里面的线。我还尝试使用 FileName 字符串格式的“${{basedir}}{0}.txt”,但无济于事。

没有错误,也没有异常。此外,如果我尝试通过配置文件配置 NLog,它可以工作,所以我也会排除权限问题。

也许是 Debug Targets not configured for logger: core 在日志文件的末尾附近,但我在其他项目中做过同样的事情并且它正在工作..

我错过了什么?有什么想法吗?

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    这是您正在寻找的方法:

    LogManager.ReconfigExistingLoggers();
    

    它将刷新所有现有的 Logger 对象,因此它们将识别您对 LoggingRules 所做的更改。

    您不应该使用此方法,因为它不会达到您的预期:

    LogManager.Configuration.Reload();
    

    只有当您使用XmlLoggingConfigurationNLog.config-file 加载配置时,它才会有用。它将再次从文件中重新读取整个配置,并返回结果(但不会应用它)。 Reload()-method 应该受到保护,而不是公共 API 的一部分(因为它会造成混淆)。

    【讨论】:

    • 谢谢,就是这样!所以这意味着在我说它正在工作的另一个项目中,这是因为我也有一个配置文件。不知道 ReconfigExistingLoggers。
    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 2023-03-07
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多