How to use log4net in C# application
来源:互联网 发布:手机动漫配音软件 编辑:程序博客网 时间:2024/06/07 11:50
/*By Jiangong SUN*/
Last Update: 08/08/2013
Here I will introduce how to configure your application to use log4net.
1) Add log4net reference
2) Add log4net in configuration file
<log4net> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log.log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <file type="log4net.Util.PatternString" value="Log-%date{yyyy-MM-dd_HH-mm-ss}.log"/> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="100MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="LogFileAppender" /> </root> </log4net>
To make it work, you should add log4net in configSections.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
3) Add Intellisense in configuration file
Firstly, download "log4net_schema.xsd" file and add ".xml" suffix to it.
Then, click menu "XML" in Visual Studio, then "Schemas"
Click "Add" and choose the xml
Or add the xsd url directly in your configuration like:
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></log4net>
Now you can use Intellisense in your configuration file
4) Usage
using log4net;using log4net.Config;namespace Test{ class Program {//initialize log private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);public static void Main(string[] args){XmlConfigurator.Configure();//usageLog.Info("log starts!");}}}
References:
http://geekswithblogs.net/MarkPearl/archive/2012/01/30/log4net-basics-with-a-console-application-c.aspx
http://www.codeproject.com/Articles/140911/log4net-Tutorial
http://logging.apache.org/log4net/release/manual/configuration.html
http://www.codeproject.com/Articles/8245/A-Brief-Introduction-to-the-log4net-logging-librar
http://sadi02.wordpress.com/2008/06/29/log4net-tutorial-in-c-net-how-can-i-show-log-in-a-file/
http://stackoverflow.com/questions/5575250/c-sharp-log4net-daily-log-file-config-usage-example
http://stackoverflow.com/questions/2976732/vs2010-using-log4net-with-intellisense
- How to use log4net in C# application
- How to use Log4net in your project
- How to use AspnetUpload™ in your web application
- Topic : How to use TabBar in viewbase application
- [Bootstrap] How to use bootstrap in your application?
- How to Reference and Use JSTL in your Web Application
- How to do logging in C# with log4net
- How to use Events in the Context of C#
- How to use composition and inheritance in visual c# ?
- Regular Expression in C# - How to make use of Expresso
- How to use C# controls
- How to use Serial Port in Windows
- How To Use Macro In Program.
- How to use virt-install in OpenSolaris
- How to use tinyfck in drupal
- How to use Regular Expression in LotusScript
- How to Use PeekMessage() Correctly in Windows
- How to use IMGDECMP.DLL in WindowsCE
- 利用Strapdown.js和markdown写极简静态博客
- S_ISREG等几个常见的宏 .
- Marvell8686 电源管理原理
- Full Screen Web Apps
- 常见的NSFileManager方法
- How to use log4net in C# application
- JAVA 多态的作用
- poj 1579 Function Run Fun(把一个递归函数变快,用数组 记忆化处理)
- 在线使用FreeTextBox编辑器时,打空格的时候出现乱码,问号(?)
- memcached完全剖析–1. memcached的基础
- AJAX包含的技术和AJAX的缺陷
- linux下java程序使用第三方jar包
- 关于学习数学
- Mapreduce 读取Hbase,写入hbase IO 不均衡问题