显式重新加载配置后 NLog 不写入文件
NLog not writing to file after explict reload of configuration
它基本上是 的副本,我知道,但即使在遵循了另一个 post 和故障排除指南之后,我仍然卡住了。
这是一个使用 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 接近日志文件的末尾,但我在其他项目中也这样做过,并且它正在运行..
我错过了什么?有什么想法吗?
这就是您正在寻找的方法:
LogManager.ReconfigExistingLoggers();
它将刷新所有现有的 Logger 对象,因此它们将识别您对 LoggingRules 所做的更改。
你不应该使用这个方法,因为它没有达到你的预期:
LogManager.Configuration.Reload();
如果您使用 XmlLoggingConfiguration
从 NLog.config
文件加载配置,它只会做一些有用的事情。它将再次从文件中重新读取整个配置,并 return 结果(但不会应用它)。 Reload()
方法应该受到保护,而不是 public API 的一部分(因为它是混淆的来源)。
它基本上是
这是一个使用 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 接近日志文件的末尾,但我在其他项目中也这样做过,并且它正在运行..
我错过了什么?有什么想法吗?
这就是您正在寻找的方法:
LogManager.ReconfigExistingLoggers();
它将刷新所有现有的 Logger 对象,因此它们将识别您对 LoggingRules 所做的更改。
你不应该使用这个方法,因为它没有达到你的预期:
LogManager.Configuration.Reload();
如果您使用 XmlLoggingConfiguration
从 NLog.config
文件加载配置,它只会做一些有用的事情。它将再次从文件中重新读取整个配置,并 return 结果(但不会应用它)。 Reload()
方法应该受到保护,而不是 public API 的一部分(因为它是混淆的来源)。