Log4net use a concise tutorial, quick look yo

Source: Internet
Author: User
Tags log4net

In the project often encounter a variety of problems, how to find the problem as soon as possible, then only rely on the log, so a system of the log is perfect and reasonable is particularly important.

In the log management plug-in log4net quite popular, the following is a brief description of how to use.

Log4net's official website is http://logging.apache.org/log4net/download_log4net.cgi

If the English is better, you can directly see the official website of the tutorial, especially the Config section of the tutorial http://logging.apache.org/log4net/release/config-examples.html

The source code download URL for the following example tutorial is: log4net Example Project 2

1. Introduction of Log4net Components

Select the project you want to add log4net in Visual Studio, right-click and choose Manage NuGet Packages

Enter the keyword LOG4, find the corresponding package, and click Install

When you are finished, you can see in the references that you have successfully referenced log4net

2, configure the log4net parameter. Because the configuration file content is more, the detailed content can download the sample project code, here is just the definition of the error log information, other similar

<Appendername= "Errorappender"type= "log4net." Appender.rollingfileappender ">      <!--The logging path, which is a relative path, the site root directory -      <filevalue= "Logs\\error\\" />      <!--logging mode, here is the Append mode -      <Appendtofilevalue= "true" />      <!--log file scrolling mode, defined here as complex mode -      <Rollingstylevalue= "Composite" />      <!--log file name rules, in conjunction with the last point, the final log files are Logs\error\2013\201309\20130927.log -      <Datepatternvalue= "Yyyy\\yyyymm\\yyyymmdd '. Log '" />      <!--The following parameters can be used directly, not to do more elaboration, mainly the above parameters, other tutorials do not -      <Staticlogfilenamevalue= "false"/>      <maxsizerollbackupsvalue= "+" />      <maximumFileSizevalue= "10MB" />      <Layouttype= "log4net." Layout.patternlayout ">        <Conversionpatternvalue= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline" />      </Layout>      <Filtertype= "log4net." Filter.levelrangefilter ">        <paramname= "Levelmin"value= "ERROR"/>        <paramname= "Levelmax"value= "ERROR"/>      </Filter>    </Appender>

3, after the configuration can be done in the program call, in order to facilitate the call, I defined a log record class Loghelper, the specific code is as follows:

usingSystem;usinglog4net;namespacewebapplication1.actionhelper{ Public classLoghelper {PrivateILog Logger;  PublicLoghelper (ILog log) { This. Logger =log; }         Public voidInfo (Objectmessage) {             This. Logger.        Info (message); }         Public voidInfo (Objectmessage, Exception e) {             This. Logger.        Info (message, E); }         Public voidDebug (Objectmessage) {             This. Logger.        Debug (message); }         Public voidDebug (Objectmessage, Exception e) {             This. Logger.        Debug (message, E); }         Public voidWarning (Objectmessage) {             This. Logger.        Warn (message); }         Public voidWarning (Objectmessage, Exception e) {             This. Logger.        Warn (message, E); }         Public voidError (Objectmessage) {             This. Logger.        Error (message); }         Public voidError (Objectmessage, Exception e) {             This. Logger.        Error (message, E); }         Public voidFatal (Objectmessage) {             This. Logger.        Fatal (message); }         Public voidFatal (Objectmessage, Exception e) {             This. Logger.        Fatal (message, E); }    }     Public classLogfactory {Staticlogfactory () {} Public Staticloghelper GetLogger (type type) {return NewLoghelper (Logmanager.getlogger (type)); }         Public StaticLoghelper GetLogger (stringstr) {            return Newloghelper (Logmanager.getlogger (str)); }    }}

Then the specific page can be called as follows

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebapplication1{ Public Partial classDefault:System.Web.UI.Page {PrivateActionhelper.loghelper log; protected voidPage_Load (Objectsender, EventArgs e)        {pageload (); }        Private voidpageload () {log= ActionHelper.LogFactory.GetLogger (Methodbase.getcurrentmethod (). Declaringtype.fullname +":"+Methodbase.getcurrentmethod ().            Name); Log. Info ("Start Page load 222"); Log. Error ("Start Page Loading"); }    }}

4, in order to save trouble, you need to capture all the anomalies in the system, you can define in global.asax

protected void Application_Error (object  sender, EventArgs e)        {            = ActionHelper.LogFactory.GetLogger ( Methodbase.getcurrentmethod (). DeclaringType);            Log. Error (" unhandled exception ", HttpContext.Current.Server.GetLastError ());        }

The setup is now complete.

All set down is actually relatively simple, if really do not understand, you can directly download: log4net example Project 21 see it.

Log4net Use a concise tutorial, take a quick look at yo

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.