Log4net usage and configuration annotation (sorting)

Source: Internet
Author: User
Tags configuration settings log4net

% D: date or time of the log output time point. You can specify a format later, for example, % d {yyyy-mm-dd hh: mm: SS}. The output is similar: 17:49:27, suitable for inserting sqlserver;
% T: name of the thread that generates the log event;
% P: log_level of the log, such as debug, warn, Or info;
% C: the output category, usually the full name of the class, such as "iNotes. Default ";
% M: log content;
% L: the location where the output log event occurs, including the category name, the thread that occurs, and the number of lines in the code.
% N output a carriage return line break. For Windows, the value is \ r \ n, and for UNIX, the value is \ n"

Let's look at the core components of log4net. log4net consists of five parts: logger, appenders, filters, layouts, and object renders.
1) Logger: logger is mainly responsible for logging. It can be recorded in multiple ways, such as files, databases, consoles, and emails;
2) appenders: mainly responsible for recording the log media. Its output methods include:
Adonetappender: records logs to the database. You can use SQL or stored procedures.

Aspnettraceappender: You can trace logs in ASP. NET.

Bufferingforwardingappender: cache log events before outputting them to sub-appenders.

Coloredconsoleappender: Write high-brightness log events in the terminal window.

Consoleappender: outputs logs to the console.

Eventlogappender: writes logs to Windows operating system logs.

Fileappender: Write logs to files.

Forwardingappender: used to specify a group of constraints for an appender.

Memoryappender: stores logs in the memory buffer.
Netsendappender: outputs logs to the Windows Messenger Service. The logs are displayed in the user terminal dialog box.
Outputdebugstringappender: configure this appender to write logs to the outputdebugstring API.
Remotingappender: writes logs to the remote acceptor through. Net remoting.
Rollingfileappender: writes logs to a file in the form of a rollback file.
Smtpappender: Write logs to emails.
Smtppickupdirappender: the configuration is similar to that of smtpappender, but replace smtphost with pickupdir.
Traceappender: Write logs to the. NET trace system.
Udpappender: Send Logs in the form of connectionless UDP multicast Rams to a remote host or broadcast logs in the form of udpclient.
Some of the above output methods may be used frequently, and some may not be used frequently. The specific examples are as follows.

3) Layouts: it is mainly responsible for formatting and outputting record logs. The display formats are as follows:

% Timestamp [% thread] %-5 level % logger {2} % NDC-% message % newline
Timestamp: the time when the program has started execution. Unit: [millisecond].
Thread: the thread that executes the current code.
Level: the log level.
Logger: the name of the log-related request.
Message: Log message.
Newline: line feed.

1) first create a Windows application project, and then add the application configuration file app. config. If it is in a web application, you need to configure the web. add references to the log4net. DLL file, and then in assemblyinfo. add the following statement to the CS file:

[Assembly: log4net. config. domconfigurator (configfileextension = "Config", watch = true)]

2) set the configuration file as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>

<! -- Log configuration -->

<Configsections>

<Section name = "log4net" type = "system. configuration. ignoresectionhandler"/>

</Configsections>

<! -- Log4net configuration settings -->

<Log4net DEBUG = "false">

<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender"> <! -- Rollingfileappender writes logs to a file in the form of a rollback file. -->

<File value = "log.txt"/> <! -- Path of the output log file -->

<Appendtofile value = "true"/> <! -- Automatically append logs to the backend when logs are output -->

<Rollingstyle value = "composite"/>

<Datepattern value = ". yyyymmdd"/> <! -- Output date format -->

<Maxsizerollbackups value = "10"/> <! -- Maximum number of logs -->

<Maximumfilesize value = "1 MB"/> <! -- Maximum capacity of a single log -->

<Staticlogfilename value = "true"/> <! -- True indicates that the file name of the latest log is always in the file section. -->

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

<Conversionpattern value = "% date %-5 level % logger-% m % N"/>

</Layout>

</Appender>

<Root>

<Level value = "Warn"/>

<Appender-ref = "rollinglogfileappender"/>

</Root>

</Log4net>

</Configuration>

% 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.
% M, % message: indicates the specific log Content
% N, % newline: line feed
 

 

 

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.