asp.net 4.0 using log4net to log examples of error logs

Source: Internet
Author: User
Tags servervariables trim log4net

It was used to log log4net, but it was used on. net2.0. The recent use of new projects is vs2015 development, to use the log4net, today will be a. NET 4.0 to use the Log4net log error logging steps to share to everyone.

1, first to http://logging.apache.org/log4net/download_log4net.cgi download the latest version of the log4net. If you do not need to modify the Log4net source code, it is recommended to download the binary system on it. The latest version is Log4net 1.2.15.

It share

2, download good Log4net.dll file, find the net4.0 version of the DLL file. Copy to the Custom directory Lib folder under the root directory in your project, and then add a reference to the file in your project.

3, find the project in the Propertites directory of the AssemblyInfo.cs file, add a line at the end of the code to invoke the configuration file is as follows:


[Assembly:log4net. Config.xmlconfigurator (configfile = @ "Config\log4net.cfg.xml", Watch = True)]

4, new Configuration folder config, and add a log4net.cfg.xml file, the contents of the configuration file is as follows:

Note: The path is not mistaken, see the above picture ~


<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "log4net" type= "System.Configuration.IgnoreSectionHandler"/>
</configSections>
<log4net>
<appender name= "Logfileappender" type= "log4net". Appender.fileappender ">
<param name= "File" value= "Log/log.txt"/>
<param name= "Appendtofile" value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Header" value= "[header]/r/n"/>
<param name= "Footer" value= "[footer]/r/n"/>
<param name= "Conversionpattern" value= "%d [%t]%-5p%c [%x]-%m%n"/>
</layout>
</appender>
<root>
<level value= "All"/>
<appender-ref ref= "Logfileappender"/>
</root>
<logger name= "Slog4net.form1" >
<level value= "All"/>
<appender-ref ref= "Logfileappender"/>
</logger>
</log4net>
</configuration>

5. Build a public class that adds code like this:

public static readonly log4net. ILog log = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);

6. Call:

Apmanage.log.de<a href= "http://www.suchso.com/programmer/chengxuyuan-duanzi-bug-shangwang.html" class= " Keylink "title=" bug fix target= "_blank" >bug</a> ("System startup");
It share

7. Record error log:

Add global processing file global.asax to the Web project.

protected void Application_Error (object sender, EventArgs e)
{
Errordeal ();
}
private void Errordeal ()
{
Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError (). GetBaseException ();
StringBuilder str = new StringBuilder ();
Str. Append ("\ r \ n" + DateTime.Now.ToString ("yyyy"). MM.DD HH:mm:ss "));
Str. Append ("\ r \ n. Customer information:");
string ip = "";
if (Request.ServerVariables.Get ("http_x_forwarded_for")!= null)
{
ip = Request.ServerVariables.Get ("Http_x_forwarded_for"). ToString (). Trim ();
}
Else
{
ip = Request.ServerVariables.Get ("REMOTE_ADDR"). ToString (). Trim ();
}
Str. Append ("\r\n\tip:" + IP);
Str. Append ("\r\n\t<a href=" projecteactual/web-server-browser-cache-1.html "class=" Keylink "title=" Browser caching Principle and caching Method " target= "_blank" > Browser </a>: "+ Request.Browser.Browser.ToString ());
Str. Append ("\r\n\t browser version:" + Request.Browser.MajorVersion.ToString ());
Str. Append ("\r\n\t Operating system:" + Request.Browser.Platform.ToString ());
Str. Append ("\ r \ n. Error message:");
Str. Append ("\r\n\t page:" + Request.Url.ToString ());
Str. Append ("\r\n\t error message:" + ex. message);
Str. Append ("\r\n\t Error Source:" + ex.) Source);
Str. Append ("\r\n\t Exception method:" + ex. TargetSite);
Str. Append ("\r\n\t Stack info:" + ex. StackTrace);
Str. Append ("\ r \ n-------------------------------------------------------------------------------------------------- ");
APManage.log.Error (str);
Finish handling the exception in time
Server.ClearError ();
}

Related Article

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.