Use log4net in VS2010

Source: Internet
Author: User
Tags log4net

*******************************************************************************
Use log4net in VS2010
Environment: Vs2010+framework4.0+log4net (1.2.10.0\framework 2.0)
Reference: http://www.cnblogs.com/llhhll/archive/2011/03/01/1967741.html
http://blog.csdn.net/lyjcn/article/details/4432833
http://blog.csdn.net/maths_bai/article/details/6074125
Http://hi.baidu.com/zonecens/blog/item/506d805184b0fc878d54306a.html
*******************************************************************************

1. Add a reference to log4net in the project

2. Compile the project, if you have already added the Log4net reference
However, when compiling, add a hint to "log4net references".
You will need to modify the "target frame" version of the current project, first
The target framework version is selected as ". NET Framework 2.0", and then
Recompile, and so on, and then you modify the "Target Frame" version
It's good to be 4.0. (Why, I don't know why.) haha

3. Add log4net to specify configuration file initialization instance
[Assembly:log4net. Config.xmlconfigurator (configfile = "Log4net.config", Watch = True)]
You can write this line of code in two places (I'm testing the WinForm program)
3.1 Application entry point, which is the class (Program.cs) where the Main function is located, adds a line on the namespace of that class
3.2 Add on Properties/assemblyinfo.cs last line

4. Modify the application configuration file app.config or Web.config
Add the following labels and contents to the next line of the <configuration> label
<configSections>
<section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler, log4net-net-2.10 "/>
</configSections>

5. Add the configuration file to see the third step we wrote that line of code
5.1 of which configfile = "Log4net.config"
In fact, it is a custom configuration file, if you do not write the log4net of those configurations in the app.config,
I think it's a bit messy.
5.2 of which Watch = True
is to let log4net automatically monitor log4net.config file changes, if the content changes you do not have to restart
The application log4net is automatically executed according to the new configuration.
5.3 Add log4net configuration file
Right key item/Add/Add New Item, select "Application configuration file", name it log4net.config, click "Add"
Note: Right-click "Properties" in the new Log4net.config file, and change ' Copy to output directory ' in the Properties window to ' copy ' if newer
Or you can manually copy the Log4net.config file to your application output directory. If you do not do this log4net component is not found
configuration file, but it will not be an error in itself is not to appear log information (enough for you to be depressed for a while).

This is a log4net configuration that can be used directly

----------------------------------------log4net.config Content starts---------------------------------------------------

<?xml version= "1.0" encoding= "Utf-8"?>
< log4net>

<root>
<level value= "All"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>

<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<!--The log is generated in the Exceptionlogfiles folder in the application root directory, and the file is named in the current log-->
<param name= "File" value= "exceptionlogfiles/"/>
<param name= "Encoding" value= "GB2312"/>
<param name= "Appendtofile" value= "true"/>
<param name= "Rollingstyle" value= "Date"/>
<param name= "Datepattern" value= "YyyyMMdd"/>
<param name= "maximumFileSize" value= "2KB"/>
<param name= "maxsizerollbackups" value= "ten"/>
<param name= "Staticlogfilename" value= "false"/>
<param name= "Datepattern" value= "Yyyymmdd&quot;. log&quot; " />
<param name= "Rollingstyle" value= "composite"/>

<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "Exception Time:%date{yy-mm-dd HH:MM:SS,FFF}%n Thread Number:%t%n exception information:%exception file:%f% N Line number:%l%n Other information:%m%n********************************************%n "/>
</layout>

<filter type= "log4net. Filter.levelrangefilter ">
</filter>

</appender>

<appender name= "Consoleappender" type= "log4net". Appender.consoleappender ">
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "Exception Time:%date{yy-mm-dd HH:MM:SS,FFF}%n Thread Number:%t%n exception information:%exception file:%f% N Line number:%l%n Other information:%m%n********************************************%n "/>
</layout>
</appender>

<appender name= "Smtpappender" type= "log4net". Appender.smtpappender ">
<to value= "dongxiaoning@tiantian.com" ></to>
<from value= "dongxiaoning@tiantian.com"/>
<subject value= "Test Log message"/>
<smtphost value= "email.tiantian.com"/>
<username value= "dongxiaoning"/>
<password value= "123456"/>
<buffersize value= "2048"/>
<!--super Long part discard-->
<lossy value= "false"/>
<!--the log of output level between warn and off-->
<filter type= "log4net. Filter.levelrangefilter ">
<param name= "Levelmin" value= "WARN"/>
<param name= "Levelmax" value= "Off"/>
</filter>

<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "Exception time:%date{yy-mm-dd HH:MM:SS,FFF}%n Thread Number:%t%n exception information:%exception location file:%f%n in line number:%l%n Other information:% M%n********************************************%n "/>
</layout>
</appender>

<appender name= "Adonetappender" type= "log4net". Appender.adonetappender ">
<!--<buffersize value= "/>-->"
<connectiontype value= "System.Data.SqlClient.SqlConnection, System.Data, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089 "/>
<connectionstring value= "Server=localhost; Uid=sa; Pwd=123456;database=log; Max Pool size=1000 "/>
<commandtext value= "INSERT into Log ([date],[thread],[level],[logger],[message]) VALUES (@log_date, @thread, @log_ Level, @logger, @message) "/>
<parameter>
<parametername value= "@log_date"/>
<dbtype value= "DateTime"/>
<!--
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date{yyyy"-' mm '-' dd HH ': ' mm ': ' SS '. ' FFF} "/>
</layout>
-->
<layout type= "log4net. Layout.rawtimestamplayout "/>
</parameter>
<parameter>
<parametername value= "@thread"/>
<dbtype value= "String"/>
<size value= "255"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%t"/>
</layout>
</parameter>
<parameter>
<parametername value= "@log_level"/>
<dbtype value= "String"/>
<size value= "/>"
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%p"/>
</layout>
</parameter>
<parameter>
<parametername value= "@logger"/>
<dbtype value= "String"/>
<size value= "255"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%c"/>
</layout>
</parameter>
<parameter>
<parametername value= "@message"/>
<dbtype value= "String"/>
<size value= "4000"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%m"/>
</layout>
</parameter>
</appender>

</log4net>

----------------------------------------End of Log4net.config content---------------------------------------------------

6. Writing in code
Log4net. ILog log = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
Log. Fatal ("Log.") Fatal ", New Exception (" Log. Fatal "));
Log. Error ("Log.") Error ", New Exception (" Log. Error "));
Log. Warn ("Log.") Warn ", New Exception (" Log. Warn "));
Log. Info ("Log.") Info ", New Exception (" Log. Info "));
Log. Debug ("Log. Debug ", New Exception (" Log. Debug "));

7. The resulting log effect is as follows

********************************************
Abnormal time: 11-08-28 18:34:13,203
Thread Number: 4412
Exception information: System.Exception: Test exception
Location file: D:\WorkTemp\TestSpace\Log4net\TestApp\Log4NetTest\Log4NetTest\Form1.cs
Line number: 38
Additional information: first parameter
********************************************

8. Summary
This is not to say that the messy configuration section is what it means, my purpose is only a "let log4net can again in the VS2010 environment normal use"
After you can use it, you go through the configuration and the effect you need is OK.
Here only the log information "written to the text file" after testing, the other needs to be studied, there is a better hope that we share.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.