Source: http://www.cnblogs.com/wangsaiming/archive/2013/01/11/2856253.html
First step: Download log4net
: http://logging.apache.org/log4net/download_log4net.cgi
Unzip the downloaded Log4net-1.2.11-bin-newkey as shown in the following:
Double-click the Bin folder
Double-click the NET folder to select different versions for. NET Framerwork
Find the appropriate version of Log4net.dll
Step Two: Apply Log4net
1. Add Log4Net.DLL references in the project
Development environment: XP Professional Service Pack3
Development tool: VS2010 flagship edition
Create a new WinForm application, as shown below for a simple demonstration
The form is named: FormMain
Copy the Log4Net.Dll to the root of the project and add the Log4net reference
After adding a reference
2, Configuration Log4net
(1) Create a new application configuration file, App. config
(2) The configuration file for App. config is as follows
1 <?xml version= "1.0"?> 2 <configuration> 3 <configSections> 4 <section name= "log4net" type= "l Og4net. Config.log4netconfigurationsectionhandler, log4net "/> 5 </configSections> 6 <log4net> 7 <!--define Output To the file--8 <appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender "> 9 <!--definition file location-->10 <file value=" log\\ "/>11 <appendtofi Le value= "true"/>12 <rollingstyle value= "Date"/>13 <datepattern value= "YYYY\\YYYYMM\\YYYYMMDD". txt ' "/>14 <staticlogfilename value=" false "/>15 <param name=" maxsizerollbackups "value=" "/>16" <layout type= "log4net. Layout.patternlayout ">17 <!--text description at the end of each log-->18 <!--output format-->19 <!--example: 2008-03-26 13 : 42:32,111 [] INFO log4netdemo.mainclass [(null)]-info-->20 <conversionpattern value= "%newline%n record time:% Date%n thread Id:[%thread]%n log level:%-5leVel%n Error Class:%logger property: [%property{ndc}]-%n error description:%message%newline%n "/>21 </layout>22 </appende r>23 <root>24 <level value= "ERROR"/>25 <!--file Form log-->26 <appender-ref ref= " Rollinglogfileappender "/>27 </root>28 </log4net>29 </configuration>
Please refer to the following link for the meaning of each parameter:
Log4net use of the explanation of Zhou Gong
http://blog.csdn.net/zhoufoxcn/article/details/2220533
Log4net use in detail to continue to reprint Zhou Gong
Http://www.cnblogs.com/zhoufoxcn/archive/2010/11/23/2515616.html
(3) Create a new Loghelper helper class, in order to make calls in multiple forms, the following is the class content:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 [Assembly:log4net. Config.xmlconfigurator (Watch = True)] 7 namespace Testlog4net 8 {9 public class LogHelper10 {//<s UMMARY>12///output log to LOG4NET13//</summary>14//<param name= "T" ></param>15 <param name= "Ex" ></param>16 #region static void Writelog (Type T, Exception ex) 17 18 public static void Writelog (Type T, Exception Ex), {log4net. ILog log = log4net. Logmanager.getlogger (t); log. Error ("Error", ex);}23 #endregion25///<summary>27///output log to Log4net28 </summary>29//<param name= "T" ></param>30//<param name= "MSG" ></par am>31 #region static void Writelog (type T, String msg), public static void Writelog (type T, string MSG) {log4net. ILog log = log4net. Logmanager.getlogger (t); Error (msg); PNs}38 #endregion40 41 42}43}
When you build the solution, you may receive the following error message:
Could not find type or namespace name ' Log4net ' (is missing a using directive or assembly reference?) Workaround
The name "log4net" workaround does not exist in the current context
At this point may be some people are very puzzled, obviously added the reference how also prompted to find the namespace. The solution to this problem is simple, right-click Project selection Properties
It is OK to modify the default. NET Framework4 Client profile of the target framework to. NET FRAMEWORK4 and then rebuild the solution.
Step Three: Test log4net
Add the following code to the event of the button
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 namespace Testlog4net11 {public partial class Formmain:form13 {+ Public formmain () InitializeComponent (); }18 private void btnTest_Click (object sender, EventArgs e) The first record is used ,//(1) FormMain is the class name x//(2) The second parameter is the string information of the Loghelper.writelog (typeof (FormMain), " Test whether the Log4net log is written "); the second record is used/ /(1) FormMain is the class name //(2) The second parameter is the exception block that needs to be caught in the //try {31 //}catch (Exception ex) { formmain// Loghelper.writelog (typeof (//}37), ex); * }41}
Run the project click the button to see the log folder at the root of the project
Open the log folder and you'll see a folder with date names open and you'll see as
Seeing this file shows that Log4net.dll's application has been successful.
Note: The name of the folder is automatically generated based on the file node you set in app. Config, and you can set the path and format you want to store yourself.
If some of the pictures do not appear, please use Chrome browser to view them.
Download log4net Test Source
C # use Log4net to log logs (GO)