现在的位置: 首页 > 综合 > 正文

c# 日志异常类

2013年12月19日 ⁄ 综合 ⁄ 共 3186字 ⁄ 字号 评论关闭
开发完一套系统,剩下的工序自然就是测试-Debug,怎么把这两道麻烦的工序变得轻松自然,最好的方法当然是每次出现异常的时候都能把异常发生的部位捕捉下来,并描述出错误异常的情况。

C#有一套非常强大的异常捕捉的方法,利用try...catch...我们可以捕捉到系统异常的信息,有经验的程序员据此信息很容易就可以判断程序的Bug所在。但是往往在实际操作中,出于成本考虑,我们组织多人同时进行的系统测试,测试人员的水平参差,对于系统异常的描述往往不容易为程序员所理解,加大了Debug的难度和沟通上的成本。

故此,我们可以将系统错误捕捉,并生成统一格式的文本日志的形式,可大大提高测试和Debug的效率。一些重现性较差的错误,也不容易漏网。

最近参考了,某系统的方法,写了一个C#的Exception日志生成类。在此与大家分享一下:

using System;
using System.Web;
using System.IO;

namespace Common
{
 /// 
 /// ExceptionLog 的摘要说明。
 /// 
 public class ExceptionLog:ApplicationException
 {
  public ExceptionLog(string message) : base(message)
  {
   Init() ;
   Log();
  }

  public ExceptionLog(Exception inner)
  {
   ex = inner;
   Init();
   Log();
  }

  public Exception ex = null;

  public override string Message 
  {
   get 
   {
    string msg=base.Message;
    if(ex != null)
    {
     msg=ex.Message;
    }
    return msg;
   }

  }

  string username=string.Empty;
  public string UserName
  {
   get { return username; }
   set { username = value; }
  }

  string userAgent = string.Empty;
  public string UserAgent 
  {
   get { return userAgent; }
   set { userAgent = value; }
  }

  string ipAddress = string.Empty;
  public string IPAddress 
  {
   get { return ipAddress; }
   set { ipAddress = value; }
  }

  string httpReferrer = string.Empty;
  public string HttpReferrer 
  {
   get { return httpReferrer; }
   set { httpReferrer = value; }
  }

  string httpVerb = string.Empty;
  public string HttpVerb 
  {
   get { return httpVerb; }
   set { httpVerb = value; }
  }

  string httpPathAndQuery = string.Empty;
  public string HttpPathAndQuery 
  {
   get { return httpPathAndQuery; }
   set { httpPathAndQuery = value; }
  }

  DateTime dateCreated;
  public DateTime DateCreated 
  {
   get { return dateCreated; }
   set { dateCreated = value; }
  } 
 
  void Init() 
  {
   DateCreated=DateTime.Now;
   if (HttpContext.Current.Request.UrlReferrer != null)
    httpReferrer = HttpContext.Current.Request.UrlReferrer.ToString();
   
   if (HttpContext.Current.Request.UserAgent != null)
    userAgent = HttpContext.Current.Request.UserAgent;
   
   if (HttpContext.Current.Request.UserHostAddress != null)
    ipAddress = HttpContext.Current.Request.UserHostAddress;

   try 
   {
    if (HttpContext.Current.Request != null
     && HttpContext.Current.Request.RequestType != null )
     httpVerb = HttpContext.Current.Request.RequestType;
   } 
   catch( Exception ex ) 
   {
    System.Diagnostics.Debug.WriteLine( ex.ToString() );
   }

   if (HttpContext.Current.Request != null
    && HttpContext.Current.Request.Url != null
    && HttpContext.Current.Request.Url.PathAndQuery != null)
    httpPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;

   if (HttpContext.Current.Request != null
    && HttpContext.Current.Request.UrlReferrer != null
    && HttpContext.Current.Request.Url.PathAndQuery != null)
    httpReferrer = HttpContext.Current.Request.UrlReferrer.ToString();
  }

  public void Log() 
  {
   string LogName=DateTime.Now.ToShortDateString()+".txt";
   string FilePath=HttpContext.Current.Request.PhysicalApplicationPath +"//Log//"+LogName;
   if (!File.Exists(FilePath)) 
   {
    using(StreamWriter sw = File.CreateText(FilePath)) 
    {
     
     sw.WriteLine("Fields           :Value");
     sw.WriteLine();
    }
   }
   using (StreamWriter sw = File.AppendText(FilePath))
   {
    sw.WriteLine("======================================");
    sw.WriteLine("DateTime         :"+this.DateCreated);
    sw.WriteLine("Message          :"+this.Message);
    sw.WriteLine("IPAddress        :"+this.IPAddress);
    sw.WriteLine("HttpReferrer     :"+this.HttpReferrer);
    sw.WriteLine("HttpVerb         :"+this.HttpVerb);
    sw.WriteLine("HttpPathAndQuery :"+this.HttpPathAndQuery);
    sw.WriteLine("UserName         :"+this.UserName);
    sw.WriteLine("UserAgent        :"+this.UserAgent);
    sw.WriteLine();
   }
  }
 }
}

调用的方法很简单:

try
   {
   ……
   }
   catch(Exception ex)
   {
    throw new Common.ExceptionLog(ex);
   }
 

抱歉!评论已关闭.