NLog检测LogLevel是否启用于目标。

huangapple go评论69阅读模式
英文:

NLog detect if LogLevel is enabled for Target

问题

以下是您提供的内容的中文翻译:

有没有办法找出特定目标是否启用了 LogLevel

情况
NLog 版本:5.1.3
平台:.Net 7
配置:XML文件通过编程配置

我们有许多嵌套的规则。
NLog 的默认行为是,如果一个规则不基于 LogLevel 匹配,它会检查是否有另一个规则可以匹配。
然而,这导致我无法禁用特定命名空间的日志记录。

用户可以通过GUI在运行时更改日志级别。这对于查找错误很重要。

为了能够禁用特定命名空间的日志记录,我们添加了 NullTarget
这接收我不想在控制台或日志文件中看到的所有日志消息。
这也运作得非常好。但是现在我们注意到,当我使用 Log.IsTraceEnabled 进行检查时,我总是得到 true,因为日志总是进入 NullTarget
这在某种程度上导致了性能问题。

因此,我想知道是否有可能从 Log.IsTraceEnabled 中排除 NullTarget

我们规则的示例:(这是规则的创建方式)

Pattern: BroadcastSuite.Common.Data.DatabaseContext*        Final: True  MinLevel: Trace MaxLevel: Fatal Targets: NullTarget   
Pattern: BroadcastSuite.Common.Data.DatabaseContext*        Final: True  MinLevel: Off   MaxLevel: Off   Targets: SplitTarget    <<<<<
Pattern: BroadcastSuite.Common.Data.PlaceholderReplacer*    Final: False MinLevel: Off   MaxLevel: Off   Targets: NullTarget   
Pattern: BroadcastSuite.Common.Data.PlaceholderReplacer*    Final: True  MinLevel: Trace MaxLevel: Fatal Targets: SplitTarget    <<<<<
Pattern: BroadcastSuite.Common.Data*                        Final: True  MinLevel: Trace MaxLevel: Trace Targets: NullTarget   
Pattern: BroadcastSuite.Common.Data*                        Final: True  MinLevel: Debug MaxLevel: Fatal Targets: SplitTarget    <<<<<
Pattern: BroadcastSuite.Common*                             Final: True  MinLevel: Trace MaxLevel: Debug Targets: NullTarget   
Pattern: BroadcastSuite.Common*                             Final: True  MinLevel: Info  MaxLevel: Fatal Targets: SplitTarget   
Pattern: BroadcastSuite*                                    Final: True  MinLevel: Trace MaxLevel: Debug Targets: NullTarget   
Pattern: BroadcastSuite*                                    Final: True  MinLevel: Info  MaxLevel: Fatal Targets: SplitTarget   
Pattern: *                                                  Final: True  MinLevel: Trace MaxLevel: Debug Targets: NullTarget   
Pattern: *                                                  Final: True  MinLevel: Info  MaxLevel: Fatal Targets: SplitTarget   

在示例中,您可以看到

  • BroadcastSuite.Common.Data* 设置为 Debug
  • BroadcastSuite.Common.Data.PlaceholderReplacer* 设置为 Trace
  • BroadcastSuite.Common.Data.DatabaseContext* 设置为 Off

(在下图中,您可以更清晰地看到)

这使我们有可能查看来自 PlaceholderReplacer 的所有内容,但不显示来自 DatabaseContext 的日志,尽管 Data 设置为 Debug

英文:

Is there any way I can find out if a LogLevel is enabled for a specific target?

Situation
NLog Version: 5.1.3
Plattfrom: .Net 7
Config: XML File and configured programmatically

We have many rules, which are nested.
The default behavior of NLog is that if a rule does not fit based on the LogLevel, it checks if another rule could match.
However, this leads to the fact that I do not have the possibility to disable the logs of a specific namespace.

The LogLevels can be changed by the user at runtime via the GUI. This is important to find errors.

So that we can disable the logs from a specific namespace, we added the NullTarget.
This takes all log messages that I don't want to see in the console or the LogFile.
This also works very well. But now we noticed that when I check with Log.IsTraceEnabled, I always get true, because the logs always go to NullTarget.
This leads partly to a performance problem.

Therefore I would like to know if it is possible to exclude the NullTarget from Log.IsTraceEnabled.

Example of our rules: (This is how the rules were created)

Pattern: BroadcastSuite.Common.Data.DatabaseContext*        Final: True  MinLevel: Trace MaxLevel: Fatal Targets: NullTarget   
Pattern: BroadcastSuite.Common.Data.DatabaseContext*        Final: True  MinLevel: Off   MaxLevel: Off   Targets: SplitTarget    <<<<<
Pattern: BroadcastSuite.Common.Data.PlaceholderReplacer*    Final: False MinLevel: Off   MaxLevel: Off   Targets: NullTarget   
Pattern: BroadcastSuite.Common.Data.PlaceholderReplacer*    Final: True  MinLevel: Trace MaxLevel: Fatal Targets: SplitTarget    <<<<<
Pattern: BroadcastSuite.Common.Data*                        Final: True  MinLevel: Trace MaxLevel: Trace Targets: NullTarget   
Pattern: BroadcastSuite.Common.Data*                        Final: True  MinLevel: Debug MaxLevel: Fatal Targets: SplitTarget    <<<<<
Pattern: BroadcastSuite.Common*                             Final: True  MinLevel: Trace MaxLevel: Debug Targets: NullTarget   
Pattern: BroadcastSuite.Common*                             Final: True  MinLevel: Info  MaxLevel: Fatal Targets: SplitTarget   
Pattern: BroadcastSuite*                                    Final: True  MinLevel: Trace MaxLevel: Debug Targets: NullTarget   
Pattern: BroadcastSuite*                                    Final: True  MinLevel: Info  MaxLevel: Fatal Targets: SplitTarget   
Pattern: *                                                  Final: True  MinLevel: Trace MaxLevel: Debug Targets: NullTarget   
Pattern: *                                                  Final: True  MinLevel: Info  MaxLevel: Fatal Targets: SplitTarget   

In the example you can see that

  • BroadcastSuite.Common.Data* is set to Debug
  • BroadcastSuite.Common.Data.PlaceholderReplacer* is set to Trace
  • BroadcastSuite.Common.Data.DatabaseContext* is set to Off.

(In the following picture, you can see it a bit clearer)

This gives us the possibility that I can see everything that comes from the PlaceholderReplacer, but the logs from DatabaseContext are not shown, even though Data is on Debug.

NLog检测LogLevel是否启用于目标。

答案1

得分: 1

以下是翻译的内容:

使用NullTarget而不是只有LoggingRule而没有任何目标(例如,writeTo=""),然后在没有配置目标时将Logger.IsTraceEnabled = false

如果没有目标,那么只需使用LoggingRule构造函数而不带Target参数(例如,默认构造函数)。如果需要,分配LoggerNamePattern并调用EnableLoggingForLevels(..)

英文:

Instead of using NullTarget then just have LoggingRule without any targets (Ex. writeTo=""), then Logger.IsTraceEnabled = false when no targets configured.

When not having a target, then just use the LoggingRule-constructor without Target-parameter (Ex. default-constructor). And if necessary assign the LoggerNamePattern and call EnableLoggingForLevels(..)

huangapple
  • 本文由 发表于 2023年5月11日 19:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定