附带详细注释的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);
相关文章推荐
- Android快速SDK(13)拍照裁剪库PhotoGraph【傻瓜模式】
- ScheduledExecutorService
- XStream双下划线问题解决与CDATA标记同时的方案
- iOS笔记之tableView
- 使用ReactiveCocoa实现iOS平台响应式编程
- Mac Android Studio 远程bindSerVice之aidl
- Unity3D之在Scene窗口显示Waypoint路径
- AndroidPN客户端源码简要分析
- android4.0 USB Camera实例(六)ffmpeg mpeg编码
- javascript中object与{}的区别
- android4.0 USB Camera实例(五补充)jpg压缩
- iOS 无线滚动相册之scrollView
- Android XUtils使用详解
- 学Android---Manifest.xml
- android4.0 USB Camera实例(五)jpg压缩
- android4.0 USB Camera实例(四)CMOS
- iOS笔记之手势
- android4.0 USB Camera实例(三)UVC
- Photoshop自动导出各尺寸Android和Iphone图标,支持新版Android Studio
- android 自定义控件方法以及实例