log4net--tools to help programmers output log information to various destinations (consoles, files, databases, etc.)

Source: Internet
Author: User
Tags log4net

1. Log4net Library is the Apache log4j framework at Microsoft. NET platform is a tool that helps programmers to output log information to various targets (consoles, files, databases, etc.).

2. The structure of the log4net is as follows:

Log4net has four main components, namely Logger (recorder), Repository (library), Appender (attachment), and layout.

3. Configuration nodes are made up of logger and appender. Logger describes which appender to use. Appender explains how logs are stored, such as writing system logs, text, databases, mail, etc.

There are two important sub-nodes in the Appender section. Filter is responsible for filtering the log level you want to get or the style that contains the special string layout that is responsible for the log output.

4. Use the steps:

4.1 Add a reference, download Log4net.dll

4.2 Configure config file:

<configuration>

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

<log4net>
<!--definition output to file--
<appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
<!--define where files are stored--
<file value= "log\\"/>
<appendtofile value= "true"/>
<rollingstyle value= "Date"/>
<datepattern value= "Yyyy\\yyyymm\\yyyymmdd '. txt '"/>
<staticlogfilename value= "false"/>
<param name= "maxsizerollbackups" value= "/>"
<layout type= "log4net. Layout.patternlayout ">
<!--output format--
<!--examples: 2008-03-26 13:42:32,111 [] INFO log4netdemo.mainclass [(null)]-info-->
<conversionpattern value= "%newline%n recording time:%date%n Response window:%logger property: [%property{ndc}]-%n recording information:%message% NewLine%n "/>
</layout>
</appender>

<!--text description at the end of each log--
<root>
<level value= "ERROR"/>
< log in as!--file-
<appender-ref ref= "Rollinglogfileappender"/>
</root>
</log4net>

</configuration>

4.3 Reality

[Assembly:log4net. Config.xmlconfigurator (watch = True)]//How do I not see the folder of a journal

Namespace Hankedemo

{

Class Loghelper

{

<summary>

Output log to Log4net

</summary>

<param name= "T" ></param>

<param name= "Ex" ></param>

#region static void Writelog (Type T, Exception Ex)

public static void Writelog (Type T, Exception Ex)

{

Log4net. ILog log = log4net. Logmanager.getlogger (t);

Log. Error ("Error", ex);

}

#endregion

<summary>

Output log to Log4net

</summary>

<param name= "T" ></param>

<param name= "MSG" ></param>

#region static void Writelog (Type T, string msg)

public static void Writelog (Type T, string msg)

{

Log4net. ILog log = log4net. Logmanager.getlogger (t);

Log. Error (msg);

}

#endregion

<summary>

Login user name of the operating system

</summary>

<returns></returns>

public static string GetUserName ()

{

try {

return System.Environment.UserName.ToUpper ();

}

Catch

{

return "UNKONWN";

}

}

}

}

4.4 Tuning Examples

Loghelper.writelog (typeof (MainForm), STRMSG);

log4net--tools to help programmers output log information to various destinations (consoles, files, databases, etc.)

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.