Use of Log plugin log4net

Source: Internet
Author: User
Tags log4net visual studio 2010

Text Format Description

The categories of logs that can be recorded include: FATAL (fatal error), error (general error), WARN (warning), info (general information), debug (debug information).

Text parameter description

%M (message): Output log message, such as Ilog.debug (...) A message for the output
%n (New line): NewLine
%d (datetime): Outputs the moment at which the current statement runs
%r (Run time): The number of milliseconds that the output program consumes from running to executing to the current statement
%T (thread ID): The current statement is in the same thread ID as%thread
%p: The current priority level of the log, which is debug, INFO, WARN ... such as
%c (Class): The name of the current log object, for example:
The pattern string is:%-10c-%m%n
The code is:
ILog Log=logmanager.getlogger ("Exam.Log");
Log. Debug ("Hello");
The output is in the following form:
Exam.log-hello
%l: The line number where the output statement is located
%F: The file name where the output statement resides
%-Number: Indicates the minimum length of the item and, if not enough, fills it with a space
%newline: Error Details
%message: Custom Output information
%logger: Class
%property: Properties

The following is a personal-defined template

%n==========
%n "Log Level"%-5level
%n "Recording Time"%date
%n "Thread Number" [%thread]
%n "Execution Time" [%r] milliseconds
%n "Error File"%F
%n "Error line number"%l
%n "class in Error"%logger property [%property{ndc}]
%n "Error description"%message
%n "Error Details"%newline

Output Example:

==========
"Log Level" ERROR
"Recording Time" 2012-11-08 15:49:37,625
"Thread Number" [10]
"Execution Time" [180] ms
"Error file" C:\Users\Administrator\Documents\Visual Studio 2010\projects\consoleapplication5\consoleapplication5\ Program.cs
"Error line number" 24
"Class in Error" Consoleapplication5.program property [(NULL)]
"Error description" error
"Error Details"
System.DivideByZeroException: Try dividing by 0.
In ConsoleApplication5.Program.Main (string[] args) location C:\Users\Administrator\Documents\Visual Studio 2010\projects\ Consoleapplication5\consoleapplication5\program.cs: Line No. 20

==========
"Log Level" ERROR
"Recording Time" 2012-11-08 15:49:37,666
"Thread Number" [10]
"Execution Time" [221] ms
"Error file" C:\Users\Administrator\Documents\Visual Studio 2010\projects\consoleapplication5\consoleapplication5\ Program.cs
"Error line number" 28
"Class in Error" Consoleapplication5.program property [(NULL)]
"Error description" errors
"Error Details"
System.Exception: An exception has occurred

Instructions for use:

Configuration file:

Node: Add in <configuration>

  <log4net> <!--definition output to file--<appender name= "Sysappender" type= "log4net. Appender.rollingfileappender "> <!--log path--<file value=" logs/log4net/"/> <!--overwrite, default is append      True--> <appendtofile value= "true"/> <rollingstyle value= "Composite"/> <!--file name-- <datepattern value= "Yyyy-mm-dd '. Log '" ></DatePattern> <!--set Unlimited backup =-1 with a maximum backup count of 1000--> <param Name= "maxsizerollbackups" value= "/>" <!--each file size-<param name= "maximumFileSize" value= "500KB"/ > <!--name can be changed to false to be able to change--<param name= "Staticlogfilename" value= "false"/> <layout type = "Log4net." Layout.patternlayout "> <!--output format--<conversionpattern value="%n==========%n "Log Level"%-5level%n "record time "%date%n" Thread number "[%thread]%n" Execution Time "[%r] milliseconds%n" error file "%f%n" error line number "%l%n" Error Class "%logger Property [%property{ndc}]%n" error description "%message%n" Error Details "%newline"/> </layout> </appender> <!--definition output to console command line--<appender name= "Consoleappender" type= "log4net. Appender.consoleappender "> <layout type=" log4net. Layout.patternlayout "> <conversionpattern value="%n==========%n "Log Level"%-5level%n "Record Time"%date%n "thread number" [%thread ]%n "Execution Time" [%r] ms%n "error file"%f%n "error line number"%l%n "Error Class"%logger Property [%property{ndc}]%n "error description"%message%n "Error Details"%newline "/> < /layout> </appender> <!--define output to Windows events--<appender name= "Eventlogappender" type= "log4net. Appender.eventlogappender "> <layout type=" log4net. Layout.patternlayout "> <conversionpattern value="%n==========%n "Log Level"%-5level%n "Record Time"%date%n "thread number" [%thread ]%n "Execution Time" [%r] ms%n "error file"%f%n "error line number"%l%n "Error Class"%logger Property [%property{ndc}]%n "error description"%message%n "Error Details"%newline "/> < /layout> </appender> <!--define the output medium of the log, the following defines the log output in four ways. You can also output the following by one type or other type. -<root> <!--document logging-->> <appender-ref ref= "Sysappender"/> <!--controlConsole control display log-<appender-ref ref= "Consoleappender"/> <!--Windows Event Log-<!--<appender-ref ref= "Eventlogappender"/>--> <!--if the corresponding logging is not enabled, you can comment out <appender-ref ref= "adonetappender_access" in this way />-</root> </log4net>

In the code:

Reference

Using Log4net;
Using System.Reflection

Add [red content] to the namespace

[Assembly:log4net. Config.xmlconfigurator (Watch = True)]
Namespace ConsoleApplication5

Invocation Example:

using system;using system.collections.generic;using system.text;using log4net;using System.Reflection; [Assembly:log4net.  Config.xmlconfigurator (Watch = True)]namespace consoleapplication5{class Program {static void Main (string[] args) {//Create log Record component instance ILog log = Log4net. Logmanager.getlogger (Methodbase.getcurrentmethod ().            DeclaringType); Log Critical error log.            Fatal ("Critical Error"); Log.            Fatal ("Critical Error", New Exception ("A fatal error has occurred"); Log error logs.            Error ("Errors"); Log.            Error ("Errors", New Exception ("An exception occurred")); Log the warning message.            Warn ("warning"); Log.            Error ("Warning", New Exception ("with a warning message")); Log general information.            Info ("General Information"); Log.            Error ("General Information", New Exception ("Send a general message")); Logs debug Information log.            Debug ("Debug Information"); Log.            Error ("Debug Information", New Exception ("A debug message has occurred"));        Console.read (); }    }}

Use of the log plug-in log4net

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.