Step by step to teach you to implement simple custom error tracking

Source: Internet
Author: User
Tags datetime error code httpcontext

One, design XML, should contain the following information:

1. DateTime: Date and time when error/exception occurred

2. File name: FileName with error/exception

3, class name: The names of the classes where the error/exception occurred

4. MethodName: The name of the method in which the error/exception occurred

5, Errormethod: The name of the function containing the error code

6, Message: Error/exception information

7. Error Details: Errors/anomalies Particulars

8, IP: Client IP Address

9, url: The URL where the error occurred

Demoxml:

<?xml version="1.0" encoding="utf-8"?>
<errorlog>
   <error>
     <datetime>datetime</datetime>
     <filename>filename</filename>
     <classname>classname</classname>
     <methodname>methodname</methodname>
     <errormethod>errormethod</errormethod>
     <messsage>ErrorMessage</messsage>
     <errordetails>Details goes here</errordetails>
     <IP>IP adress</IP>
     <url>URL</url>
   </error>
</errorlog>

Second, the design error processing class: ErrorHandler.cs, inside the Writeerror method needs exception and filename two parameters:

ErrorHandler.cs

using System;


using System.Collections.Generic;


using System.Text;


using System.Xml;


using System.Reflection;


using System.Diagnostics;


namespace Code_center


{


public class ErrorHandler


     {


string _strerrormessage, _strdetails, _strclassname, _strmethodname;


datetime _dtoccurancetime = new DateTime ();


Public ErrorHandler ()


         {


         }


Public ErrorHandler (DateTime time, String className, String methodname,


string errormessage, string details)


         {


_dtoccurancetime = time;


_strclassname = ClassName;


_strdetails = details;


_strerrormessage = errormessage;


_strmethodname = methodname;


         }


public static void Writeerror (Exception ex)


         {


Writeerror (ex, "");


         }


public static void Writeerror (Exception ex, string fileName)


         {


XmlDocument doc = new XmlDocument ();


string strrootpath =


system.configuration.configurationmanager.appsettings["LogFilePath"]. ToString ();


string xmlpath = System.Web.HttpContext.Current.Server.MapPath (Strrootpath);


Doc. Load (@xmlPath);


XmlNode Newxmlnode, Oldxmlnode;


Oldxmlnode = doc. CHILDNODES[1]. Childnodes[0];


Newxmlnode = Oldxmlnode.clonenode (true);


StackTrace stacktrace = new StackTrace ();


stackframe stackframe = stacktrace.getframe (1);


MethodBase methodbase = Stackframe.getmethod ();


Newxmlnode.childnodes[0]. innertext = DateTime.Now.ToString ();


Newxmlnode.childnodes[1]. innertext = FileName;


newxmlnode.childnodes[2]. innertext = MethodBase.DeclaringType.FullName;


Newxmlnode.childnodes[3]. innertext = Methodbase.name;


Newxmlnode.childnodes[4]. InnerText = ex. Targetsite.name;


Newxmlnode.childnodes[5]. InnerText = ex. message;


Newxmlnode.childnodes[6]. InnerText = ex. StackTrace;


NEWXMLNODE.CHILDNODES[7]. innertext = System.Web.HttpContext.Current.Request.UserHostAddress;


Newxmlnode.childnodes[8]. innertext = System.Web.HttpContext.Current.Request.Url.OriginalString;


Doc. CHILDNODES[1]. AppendChild (Newxmlnode);


Doc. Save (@xmlPath);


Doc. RemoveAll ();


         }


     }


}

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.