Use log4net in C # (WinForm program

Source: Internet
Author: User
Tags log4net

Http://www.csharpwin.com/csharpspace/678.shtml

1. Download log4net (Google log4net)

2.unzip log4net

3. Run VS, create a new C # Windows application.

4. Add Reference log4net

5. Create a new application configuration file, App. config (details attached)

6. Open Form1.cs, 

adds a row [namespace] on assembly:log4net. Config.domconfigurator (watch=true)] 
(or edit the Assembly.cs file, add the following: Span class= "Apple-converted-space" > 
[Assembly:log4net. Config.domconfigurator (configfileextension= "config", Watch=true)])  

Adds a static variable to the class Form1  

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



7. Add a button. Add a row of logs to the button handler. Warn ("Hello! ");  



8. Run the program. Click the button.  



OK, open bin\debug\log-file.txt and you can see "Hello".  





attached. app.config 

Actual use:

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

<configuration>

<configSections>

<section name= "log4net" type= "System.Configuration.IgnoreSectionHandler"/>

</configSections>

<appSettings>

</appSettings>

<log4net>

<appender name= "Logfileappender" type= "log4net. Appender.rollingfileappender ">

<file value= "dmhjpgrklog. Log"/> file name

<appendtofile value= "true"/>

<rollingstyle value= "Size"/>

<maxsizerollbackups value= "3"/>

<maximumfilesize value= "30MB"/>

<staticlogfilename value= "true"/>

<datepattern value= "Yyyymmdd-hh:mm:ss"/>

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%date[tid:%-5thread][%level][%logger"%message%newline "/>

</layout>

</appender>

<appender name= "Consoleappender" type= "log4net. Appender.consoleappender ">

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>

</layout>

</appender>

<root>

<level value= "All"/>

<appender-ref ref= "Logfileappender"/>

<appender-ref ref= "Consoleappender"/>

</root>

</log4net>

</configuration>

AssemblyInfo.cs

Add Log4net to this project

[Assembly:log4net. Config.xmlconfigurator (configfile = "RK. config", Watch = True)]

Call------------------------------------------------------------


Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Text;

Using System.Windows.Forms;

Using Log4net;



[Assembly:log4net. Config.domconfigurator (Watch = True)]

Namespace WindowsApplication1

{





public partial class Form1:form&NBSP;

{&NBSP;

Public Form1 () &NBSP;

{&NBSP;

InitializeComponent (); &NBSP;

}&NBSP;





private static readonly log4net. ILog log = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType); &NBSP;





private void Button1_Click (object sender, EventArgs e) &NBSP;

{&NBSP;

Log. Warn ("Hello! "); &NBSP;



}&NBSP;

}

Related Article

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.