c# 自定义异常类

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

namespace Common
{
    /// <summary>
    /// ExceptionLog 的摘要说明。
    /// </summary>
    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);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值