Log4net usage details)

Source: Internet
Author: User
Tags log4net

Note: ThisProgramDemonstrate how to use log4net to record program log information. Log4net is a well-known open-source logging component. Log4net allows you to easily record log information to files, consoles, Windows event logs, and databases (including ms SQL Server, access, Oracle9i, Oracle8i, DB2, and SQLite. In addition, we can also control the log level to be recorded. The log types include fatal (fatal error), error (general error), and warn (warning), Info (general information), debug (debugging information ). To obtain the latest log4net Component Library, go to the official website http://logging.apache.org/log4net/to download it. The latest version is 1.2.10.

The following example shows how to use log4net to record logs.

Download the latest version of log4net from the official website. The latest version is 1.2.10. In the program, we only need the log4net. dll file. You can use it in the program by adding a reference to log4net. dll.

Then we configure the relevant configuration file (winform corresponds *. EXE. config, webform corresponds *. config). The console application is used in this instance. The configuration is as follows (with descriptions of each configuration ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "system. configuration. ignoresectionhandler"/>
</Configsections>
<Deleetask>
</Appsettings>
<Log4net>
<! -- Define output to file -->
<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<! -- Define the file storage location -->
<File value = "D: log4netfile.txt"/>
<Appendtofile value = "true"/>
<Rollingstyle value = "date"/>
<Datepattern value = "yyyymmdd-hh: mm: SS"/>
<Layout type = "log4net. layout. patternlayout">
<! -- Text description at the end of each log -->
<Footer value = "by Zhou Gong"/>
<! -- Output format -->
<! -- Example: 13:42:32, 111 [10] info log4netdemo. mainclass [(null)]-info -->
<Conversionpattern value = "record time: % date thread ID: [% thread] Log Level: %-5 level error class: % logger property: [% property {NDC}]-error Description: % message % newline "/>
</Layout>
</Appender>
<! -- Define output to console command line -->
<Appender name = "leleappender" type = "log4net. appender. consoleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>
<! -- Define output to Windows events -->
<Appender name = "eventlogappender" type = "log4net. appender. eventlogappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>
<! -- Define the output to the database. Here, for example, the output to the access database. The database is log4net. mdb of drive c -->
<Appender name = "adonetappender_access" type = "log4net. appender. adonetappender">
<Connectionstring value = "provider = Microsoft. Jet. oledb.4.0; Data Source = C: log4net. mdb"/>
<Commandtext value = "insert into logdetails ([logdate], [thread], [level], [logger], [Message]) values (@ logdate, @ thread, @ loglevel, @ logger, @ message) "/>
<! -- Define parameters -->
<Parameter>
<Parametername value = "@ logdate"/>
<Dbtype value = "string"/>
<Size value = "240"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ thread"/>
<Dbtype value = "string"/>
<Size value = "240"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% thread"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ loglevel"/>
<Dbtype value = "string"/>
<Size value = "240"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% level"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ logger"/>
<Dbtype value = "string"/>
<Size value = "240"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% logger"/>
</Layout>
</Parameter>
<Parameter>
<Parametername value = "@ message"/>
<Dbtype value = "string"/>
<Size value = "240"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% message"/>
</Layout>
</Parameter>
</Appender>
<! -- Define the log output media. The following defines four log output methods. You can also output data of the following type or other types. -->
<Root>
<! -- Log recorded as a file -->
<Appender-ref = "logfileappender"/>
<! -- Console control display log -->
<Appender-ref = "leleappender"/>
<! -- Windows event log -->
<Appender-ref = "eventlogappender"/>
<! -- If the corresponding logging is not enabled, comment out the logs in this way.
<Appender-ref = "adonetappender_access"/>
-->
</Root>

</Log4net>
</Configuration> program file:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Windows. forms;
Using system. reflection;
Using log4net;

// Note that the following statement must be added to specify that log4net uses the. config file to read configuration information.
// If it is winform(the hypothetical program is mydemo.exe, A mydemo.exe. config file will be created)
// If it is a webform, the relevant information is read from web. config.
[Assembly: log4net. config. xmlconfigurator (watch = true)]
Namespace log4netdemo
{
/// <Summary>
/// Description: This program demonstrates how to use log4net to record program log information. Log4net is a well-known open-source logging component.
/// Use log4net to easily record log information to files, consoles, Windows event logs, and databases (including ms SQL Server, access, Oracle9i, Oracle8i, DB2, and SQLite ).
/// The following example shows how to use log4net to record logs.
/// By Zhou Gong
/// Time:
/// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/03/26/2220533.aspx
/// </Summary>
Public class mainclass
{
Public static void main (string [] ARGs)
{
// Application. Run (New mainform ());
// Create a logging component instance
Ilog log = log4net. logmanager. getlogger (methodbase. getcurrentmethod (). declaringtype );
// Record error logs
Log. Error ("error", new exception ("an exception occurred "));
// Record serious errors
Log. Fatal ("Fatal", new exception ("a fatal error occurred "));
// Record general information
Log. Info ("info ");
// Record debugging information
Log. debug ("debug ");
// Record warning information
Log. Warn ("Warn ");
Console. writeline ("logging is complete. ");
Console. Read ();
}
}
}
Running result:

Console output

Log File Content

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.