errorHandler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kirin
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                throw new Exception("Custom error");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                kirin.errorHandler.WriteError(ex, "Default.aspx.cs");
            }
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;

namespace kirin
{
    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();
        }
    }

}

 

 

 

 

 

 

<?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>
  <error>
    <datetime>2010-1-29 9:29:24</datetime>
    <filename>Default.aspx.vb</filename>
    <classname>kirin._Default</classname>
    <methodname>Page_Load</methodname>
    <errormethod>Page_Load</errormethod>
    <messsage>Custom error</messsage>
    <errordetails>   在 kirin._Default.Page_Load(Object sender, EventArgs e) 位置 C:/Demo/kirin_code_center/kirin/Default.aspx.cs:行号 16</errordetails>
    <IP>127.0.0.1</IP>
    <url>http://localhost:2192/default.aspx</url>
  </error>
  <error>
    <datetime>2010-1-29 9:29:24</datetime>
    <filename>Global.asax</filename>
    <classname>kirin.Global</classname>
    <methodname>Application_Error</methodname>
    <errormethod>GetFileInfo</errormethod>
    <messsage>文件不存在。</messsage>
    <errordetails>   在 System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
   在 System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
   在 System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
   在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)</errordetails>
    <IP>127.0.0.1</IP>
    <url>http://localhost:2192/favicon.ico</url>
  </error>
</errorlog>

你可能感兴趣的:(exception,String,object,Class,callback,encoding)