程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 分享一個收集系統出現錯誤時收集信息的類

分享一個收集系統出現錯誤時收集信息的類

編輯:C#入門知識

在系統開發過程中,出現錯誤在所難免,但是怎麼樣處理好出錯的情況,以及盡可能地收集出錯時的一些信息,對我們分析錯誤,從而查找排除錯誤是很有幫助的,下面提供一個錯誤信息收集類給大家,希望能夠派上用場。
/// <summary>
    /// Class that handles gathering of error information
    /// for reporting purposes
    /// </summary>
    public static class ErrorManager
    {
        #region Public Static Functions

        /// <summary>
        /// returns information specific to ASP.Net/IIS (Request, Response, Cache, etc.)
        /// </summary>
        /// <returns>An HTML formatted string containing the ASP.Net information</returns>
        public static string GetAllASPNetInformation()
        {
            StringBuilder Builder = new StringBuilder();
            HttpContext Current = HttpContext.Current;
            Builder.Append("<strong>Request Variables</strong><br />");
            Builder.Append(Current.Request.DumpRequestVariable());
            Builder.Append("<br /><br /><strong>Response Variables</strong><br />");
            Builder.Append(Current.Response.DumpResponseVariable());
            Builder.Append("<br /><br /><strong>Server Variables</strong><br />");
            Builder.Append(Current.Request.DumpServerVars());
            Builder.Append("<br /><br /><strong>Session Variables</strong><br />");
            Builder.Append(Current.Session.DumpSession());
            Builder.Append("<br /><br /><strong>Cookie Variables</strong><br />");
            Builder.Append(Current.Request.Cookies.DumpCookies());
            Builder.Append("<br /><br /><strong>Cache Variables</strong><br />");
            Builder.Append(Current.Cache.DumpCache());
            Builder.Append("<br /><br /><strong>Application State Variables</strong><br />");
            Builder.Append(Current.Application.DumpApplicationState());
            return Builder.ToString();
        }

        /// <summary>
        /// Gets assembly information for all currently loaded assemblies
        /// </summary>
        /// <returns>An HTML formatted string containing the assembly information</returns>
        public static string GetAssemblyInformation()
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<strong>Assembly Information</strong><br />");
            AppDomain.CurrentDomain.GetAssemblies().ForEach<Assembly>(x => Builder.Append(x.DumpProperties()));
            return Builder.ToString();
        }

        /// <summary>
        /// Gets information about the system.
        /// </summary>
        /// <returns>An HTML formatted string containing the state of the system.</returns>
        public static string GetSystemInformation()
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<strong>System Information</strong><br />");
            Builder.Append(System.Type.GetType("Utilities.Environment.Environment").DumpProperties());
            return Builder.ToString();
        }

        /// <summary>
        /// Gets all process information and outputs it to an HTML formatted string
        /// </summary>
        /// <returns>An HTML formatted string containing the process information</returns>
        public static string GetProcessInformation()
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<strong>Process Information</strong><br />");
            Builder.Append(Process.GetProcesses().GetInformation());
            return Builder.ToString();
        }

        #endregion
    }


摘自 weizhiai12的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved