> php教程 > php手册 > 본문

c# 自定义异常类

WBOY
풀어 주다: 2016-06-06 20:01:05
원래의
1086명이 탐색했습니다.

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

 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);
}

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!