Simple use of log4net

Source: Internet
Author: User
Tags log4net

In the previous essay, the exception handler has been in the filter, we have written the error message to the queue, and then a separate thread to the queue error information processing, the error message output to the developed file, but if we need to change the requirements, we intend to export the error information to the database, At this point, we need to rewrite the exported code, it is troublesome, so we can use a log processing component log4net, we only need to complete the configuration file in the corresponding configuration, we can achieve the above functions. Log4net is used to record the log, the program can be run in the process of information output to some places (files, databases, eventlog, etc.), the log is the program's black box, can be
The log looks at the system's running process to discover problems with the system. The role of the log: the process of running the steps, the success of the failure to record, the critical data recorded down the system problems.

Use of log4net

1. Add Reference Log4net.dll

2. Configure the Web. Config file

For example, the configuration for saving log information to a document is as follows:

<configSections> <!--log4net configuration--<section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler, Log4net"/> </configSections> <!--log4net configuration-<log4net> <!--OFF, FATAL, ERROR, WARN, INFO, DE BUG, All--<!--Set root logger level to ERROR and its appenders-<root> < Level value=" All"/> <appender-ref ref="Sysappender"/> </root> <!--Print Only messages of the level DEBUG or aboveinchThe packages--and <logger name="Weblogger"> <level value="ERROR"/> </logger> <appender name="Sysappender"Type="log4net. Appender.rollingfileappender,log4net"> <param name="File"Value="app_data/"/> <param name="Appendtofile"Value="true"/> <param name="Rollingstyle"Value="Date"/> <param name="Datepattern"Value="&quot; Logs_&quot;yyyymmdd&quot;. txt&quot;"/> <param name="Staticlogfilename"Value="false"/> <layout type="log4net. Layout.patternlayout,log4net"> <param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n"/> <param name="Header"Value="& #13;& #10,----------------------header--------------------------& #13;& #10;"/> <param name="Footer"Value="& #13;& #10,----------------------footer--------------------------& #13;& #10;"/> </layout> </appender> <appender name="ConsoleApp"Type="log4net. Appender.consoleappender,log4net"> <layout type="log4net. Layout.patternlayout,log4net"> <param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n"/> </layout> </appender> </log4net> <!--log4net configuration ends-

(3) The log system must read the configuration file and create the processing class at the beginning of the program, so it should be added at the beginning of the Application_Start of the global file.

Log4net. Config.XmlConfigurator.Configure ();

(4) Then on a separate thread, create a log processing class, and then output the error message through the log component

// write the error to the log to                            fetch the // File.appendalltext (FilePath + DateTime.Now.ToString ("yyyy-mm-dd") + ". txt", ex. ToString (), System.Text.Encoding.UTF8);                             // If the project wants to write the log in a database or other way instead of writing it in a file, then we are going to modify the project code, so using the logging component log4net, just modify the configuration file can be very simple implementation                            ILog logger = Logmanager.getlogger ("errormsg");                            Logger. Error (ex);

Simple use of 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.