C# log4net 层次结构和日志记录级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8926409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4net hierarchy and logging levels
提问by Siva
This sitesays
这个网站说
Loggers may be assigned levels. Levels are instances of the log4net.Core.Level class. The following levels are defined in order of increasing priority:
- ALL
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
- OFF
记录器可以被分配级别。级别是 log4net.Core.Level 类的实例。以下级别按优先级递增的顺序定义:
- 全部
- 调试
- 信息
- 警告
- 错误
- 致命的
- 离开
DEBUG seems to have lowest priority and ERROR is higher.
DEBUG 的优先级似乎最低,而 ERROR 的优先级更高。
Question
题
- If I set Min and Max example DEBUG and ERROR it prints everthing DEBUG, INFO, WARN and ERROR. Without use of min and max filter. If I specify ERROR (Logging level = ERROR) Will it include DEBUG, INFO & WARN
- 如果我设置 Min 和 Max 示例 DEBUG 和 ERROR,它会打印所有调试、信息、警告和错误。不使用最小和最大过滤器。如果我指定 ERROR (Logging level = ERROR) 会包括 DEBUG、INFO 和 WARN
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="ERROR"/>
</filter>
Instead of min and max filter. Is it possible to configure a level and include all other levels below it for logging.
而不是最小和最大过滤器。是否可以配置一个级别并在其下方包含所有其他级别以进行日志记录。
Example - Set level as Error it will include DEBUG, INFO, WARN and ERROR. Is this possible with log4net?
示例 - 将级别设置为错误,它将包括调试、信息、警告和错误。这可以用 log4net 实现吗?
Posting log4net config based on one of comments:
根据评论之一发布 log4net 配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections >
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<layout type="log4net.Layout.XMLLayout" /> -->
<param name="File" value="TestLog.log" />
<param name="AppendToFile" value="false" />
<layout type="log4net.Layout.PatternLayout">
<header type="log4net.Util.PatternString" value="[START LOG] %newline" />
<footer type="log4net.Util.PatternString" value="[END LOG] %newline" />
<conversionPattern value="%d [%t] %-5p - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="ERROR"/>
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<logger name="log4NetExample">
<!-- <appender-ref ref="B" /> -->
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
</log4net>
回答by Ilya Kogan
For most applications you would like to set a minimum level but not a maximum level.
对于大多数应用程序,您希望设置最低级别而不是最高级别。
For example, when debugging your code set the minimum level to DEBUG, and in production set it to WARN.
例如,在调试代码时将最低级别设置为 DEBUG,而在生产中将其设置为 WARN。
回答by weismat
DEBUG will show all messages, INFO all besides DEBUG messages, and so on.
Usually one uses either INFO or WARN. This dependens on the company policy.
DEBUG 将显示所有消息,除了 DEBUG 消息之外的所有信息,等等。
通常使用 INFO 或 WARN。这取决于公司政策。
回答by ulty4life
As others have noted, it is usually preferable to specify a minimum logging level to log that level and any others more severe than it. It seems like you are just thinking about the logging levels backwards.
正如其他人所指出的,通常最好指定一个最低日志记录级别来记录该级别以及比它更严重的任何其他级别。似乎您只是在向后考虑日志记录级别。
However, if you want more fine-grained control over logging individual levels, you can tell log4net to log only one or more specific levels using the following syntax:
但是,如果您想对记录单个级别进行更细粒度的控制,则可以使用以下语法告诉 log4net 仅记录一个或多个特定级别:
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="WARN"/>
</filter>
Or to exclude a specific logging level by adding a "deny" node to the filter.
或者通过向过滤器添加“拒绝”节点来排除特定的日志记录级别。
You can stack multiple filters together to specify multiple levels. For instance, if you wanted only WARN and FATAL levels. If the levels you wanted were consecutive, then the LevelRangeFilter is more appropriate.
您可以将多个过滤器堆叠在一起以指定多个级别。例如,如果您只想要 WARN 和 FATAL 级别。如果您想要的级别是连续的,那么 LevelRangeFilter 更合适。
Reference Doc: log4net.Filter.LevelMatchFilter
参考文档:log4net.Filter.LevelMatchFilter
If the other answers haven't given you enough information, hopefully this will help you get what you want out of log4net.
如果其他答案没有给你足够的信息,希望这能帮助你从 log4net 中得到你想要的。
回答by barrypicker
Its true the official documentation (Apache log4net? Manual - Introduction) states there are the following levels...
官方文档(Apache log4net?手册 - 介绍)确实有以下级别......
- ALL
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
- OFF
- 全部
- 调试
- 信息
- 警告
- 错误
- 致命的
- 离开
... but oddly when I view assembly log4net.dll, v1.2.15.0 sealed class log4net.Core.Level I see the following levels defined...
...但奇怪的是,当我查看程序集 log4net.dll, v1.2.15.0 密封类 log4net.Core.Level 时,我看到定义了以下级别...
public static readonly Level Alert;
public static readonly Level All;
public static readonly Level Critical;
public static readonly Level Debug;
public static readonly Level Emergency;
public static readonly Level Error;
public static readonly Level Fatal;
public static readonly Level Fine;
public static readonly Level Finer;
public static readonly Level Finest;
public static readonly Level Info;
public static readonly Level Log4Net_Debug;
public static readonly Level Notice;
public static readonly Level Off;
public static readonly Level Severe;
public static readonly Level Trace;
public static readonly Level Verbose;
public static readonly Level Warn;
I have been using TRACE in conjunction with PostSharp OnBoundaryEntry and OnBoundaryExit for a long time. I wonder why these other levels are not in the documentation. Furthermore, what is the true priority of all these levels?
很长一段时间以来,我一直将 TRACE 与 PostSharp OnBoundaryEntry 和 OnBoundaryExit 结合使用。我想知道为什么这些其他级别不在文档中。此外,所有这些级别的真正优先级是什么?
回答by Mohamed-ali MAAMAR
Here is some code telling about priority of all log4net levels:
这是一些说明所有 log4net 级别的优先级的代码:
TraceLevel(Level.All); //-2147483648
TraceLevel(Level.Verbose); // 10 000
TraceLevel(Level.Finest); // 10 000
TraceLevel(Level.Trace); // 20 000
TraceLevel(Level.Finer); // 20 000
TraceLevel(Level.Debug); // 30 000
TraceLevel(Level.Fine); // 30 000
TraceLevel(Level.Info); // 40 000
TraceLevel(Level.Notice); // 50 000
TraceLevel(Level.Warn); // 60 000
TraceLevel(Level.Error); // 70 000
TraceLevel(Level.Severe); // 80 000
TraceLevel(Level.Critical); // 90 000
TraceLevel(Level.Alert); // 100 000
TraceLevel(Level.Fatal); // 110 000
TraceLevel(Level.Emergency); // 120 000
TraceLevel(Level.Off); //2147483647
private static void TraceLevel(log4net.Core.Level level)
{
Debug.WriteLine("{0} = {1}", level, level.Value);
}
回答by Mel Pama
This might help to understand what is recorded at what level Loggers may be assigned levels. Levels are instances of the log4net.Core.Level class. The following levels are defined in order of increasing severity - Log Level.
这可能有助于了解记录在什么级别记录的内容可以为 Logger 分配级别。级别是 log4net.Core.Level 类的实例。以下级别按严重性递增的顺序定义 - 日志级别。
Number of levels recorded for each setting level:
每个设置级别记录的级别数:
ALL DEBUG INFO WARN ERROR FATAL OFF
?All
?DEBUG ?DEBUG
?INFO ?INFO ?INFO
?WARN ?WARN ?WARN ?WARN
?ERROR ?ERROR ?ERROR ?ERROR ?ERROR
?FATAL ?FATAL ?FATAL ?FATAL ?FATAL ?FATAL
?OFF ?OFF ?OFF ?OFF ?OFF ?OFF ?OFF
回答by Dhananjay
Try like this, it worked for me
像这样尝试,它对我有用
<root>
<!--<level value="ALL" />-->
<level value="ERROR" />
<level value="INFO" />
<level value="WARN" />
</root>
This logs 3 types of errors - error, info, and warning
这会记录 3 种类型的错误 - 错误、信息和警告

