您的位置:首页 > 移动开发

附带详细注释的log4net的app.config文件配置例子

2015-11-11 11:13 489 查看
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>

<!--
In a config file where there will (potentially) be more information stored beyond just the log4net configuration information, you will need to specify a
section to identify where the log4net configuration is housed. Here is a sample section that specifies that the configuration information will be stored
under the XML tag "log4net"
-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

<log4net>
<!--
You need to have one root section to house your top-level logger references. These are the loggers that inherit information from your base logger (root).
The only other thing that the root section houses is the minimum level to log. Since everything inherits from the root, no appenders will log information below
that specified here. This is an easy way to quickly control the logging level in your application. Here is an example with a default level of
INFO (which means DEBUG messages will be ignored) and a reference to two appenders that should be enabled under root:
-->
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>

<!--
Sometimes you will want to know more about a particular part of your application. log4net anticipated this by allowing you to specify additional logger
references beyond just the root logger.
Note that the logger name is the full name of the class including the namespace. If you wanted to monitor an entire namespace, it would be as simple as listing
just the namespace you wanted to monitor. I would recommend against trying to re-use appenders in multiple loggers. It can be done, but you can get some
unpredictable results.
Set logger name to * to target entire application
-->
<logger name="*">
<!--<level value="DEBUG"/>-->
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</logger>

<!--
An appender is the name for what logs the information. It specifies where the information will be logged, how it will be logged, and under what circumstances
the information will be logged. While each appender has different parameters based upon where the data will be going, there are some common elements.
The first is the name and type of the appender. Each appender must be named (anything you want) and have a type assigned to it (specific to the type of
appender desired)
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<!--
Inside of each appender must be a layout section. This may be a bit different depending on the type of appender being used, but the basics are the same.
You need a type that specifies how the data will be written. There are multiple options, but the one that I suggest you use is the pattern layout type.
This will allow you to specify how you want your data written to the data repository. If you specify the pattern layout type, you will need a sub-tag that
specifies a conversion pattern. This is the pattern by which your data should be written to the data repository.
-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"/>
</layout>

<!-- Rolling File Appender specific settings. E.g. The name of the text file has to be specified -->
<file value="c:\ApplicationLog\" />
<appendToFile value="true" />
<rollingStyle value="Date"/>
<datePattern value="yyyy-MM-dd'.log'" />
<maxSizeRollBackups value="90" />
<staticLogFileName value="false" />

</appender>

</log4net>

<system.diagnostics>
<trace autoflush="true">
<!-- Output the log4net internal debug messages by adding a trace listener -->
<listeners>
<add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\ApplicationLog\log4net.log" />
</listeners>
</trace>
</system.diagnostics>

</configuration>


配置文件设置完成后,需要引入下面三行代码

1. 在被跟踪的类外面加上:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]


2. 在被跟踪的类中加上以下代码,创建私有的ILog实例

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


3. 在需要写log的地方,添加追加log的具体实现代码

log.Info("Initlaized");




log.Error("Error occured while initializing", ex);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: