Example of using Asp.net + winform + log4net

Source: Internet
Author: User

Log4net is an open-source log component launched by apache.org Based on log4j for. net programs.

The latest version of log4net is 1.2.10. log4net supports many log storage methods, including:

  • Save to SQL Server database
  • Save to access database
  • Save to Oracle Database
  • Store data to a DB2 database
  • Store to sqllite Database
  • Generate logs in the trace mode of Asp.net
  • Cached logs
  • Output directly to the console screen (color supported)
  • Store system logs
  • Generate a single file log
  • Generate multi-file logs
  • Stored in memory
  • Use Net send to a specified network domain or host
  • Remote log generation
  • Use SMTP to Send Logs to a specified mailbox
  • Use SMTP of IIS to put logs into the mail directory
  • Use UDP to Send Logs

 

The specific configuration details of various log storage methods above are shown here

Log4net log records are divided into 7 levels, from high to low: Off, fatal, error, warn, info, debug, all

To use log4net, you must specify the configuration file for log4net. There are two methods:

  1. Put web. config or app. config
  2. Put a separate configuration file, such as log4net. config (recommended)

 

The configuration format of log4net is roughly as follows:

<?xml version="1.0" encoding="utf-8" ?><log4net debug="false">    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"><file value="log.txt" /><appendToFile value="true" /><rollingStyle value="Composite" /><datePattern value=".yyyyMMdd" /><maxSizeRollBackups value="10" /><maximumFileSize value="1MB" /><staticLogFileName value="true" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date %-5level %logger -%m%n" /></layout>    </appender>    <root>        <level value="WARN" />        <appender-ref ref="RollingLogFileAppender" />    </root></log4net>

The preceding figure shows an independent log4net configuration file:

<Log4net> </log4net> is the root, <root> </root> is required, and is the root of all log formats. It defines the basic level of log records, and the currently enabled logging method, the above appender will inherit the root level

The type in appender specifies the method used to record logs. For specific values, go here to view

Layout is the definition of the log output format.:

% D, % Date: Time of course

% Level: indicates the log level.

% Logger, % C: indicates the topic generated by the log, which is usually the class name to locate.

% T, % thread: Process

% L: indicates the number of lines of the Code that generates logs.

% M, % message: indicates the specific log Content

% N, % newline: line feed

-------------------------

I configured a multi-file log. A log file is generated every day. The log name isLog.txt. 20081228Format

 

The following describes the specific steps:

1. Add log4net. dll reference

 

2.Web ApplicationAndWinform

InAssemblyinfo. CS, Add

[Assembly: log4net. config. xmlconfigurator (configfile = "log4net. config ", watch = true)] // log4net. config is the name of the configuration file. You can set it by yourself. // Watch indicates whether to monitor changes to the configuration file during the running of the program.

3. For a website project, add

Protected void application_start (Object sender, eventargs e) {// configure log4net log4net. config. xmlconfigurator. Configure (new system. Io. fileinfo ("log4net. config "));}

4. Actual call

Class program {Private Static readonly ilog log = logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype); // system. reflection. methodbase. getcurrentmethod (). declaringtype // obtain the type of the current class by reflection and record it in the log to facilitate locating the static void main (string [] ARGs) {log. error ("this is an error log"); log. fatal ("this is a fatal error log"); log. warn ("this is a warning log"); log. info ("this is a normal message"); console. read ();}}

The following is a log generated:

 

Download complete example code

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.