搜尋
首頁php教程php手册c# 自定义异常类

c# 自定义异常类

Jun 06, 2016 pm 08:01 PM
phpsystemusing例外自訂

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

 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)