Asp.net錯誤記錄檔處理

來源:互聯網
上載者:User

 

using System; 

using System.Configuration; 

using System.IO; 

using System.Web; 

 

namespace SingingEels 



    public class ErrorLogger 

    { 

        public static void LogException(Exception ex) 

        { 

ErrorLoggerSection configInstance = ConfigurationManager.GetSection("WebSiteErrorLogger") as ErrorLoggerSection; 

 

            if (configInstance != null) 

            { 

string myDirectory=HttpContext.Current.Server.MapPath(configInstance.LogFilePath + DateTime.Now.Year.ToString()+"/"+ DateTime.Now.Month.ToString()+"/"); 

string mypath=myDirectory+DateTime.Now.ToShortDateString()+".txt"; 

if (!Directory.Exists(myDirectory)) 



Directory.CreateDirectory(myDirectory); 



File.AppendAllText(mypath, string.Format("{0:yyyyMMdd_HHmmss} :: {1}{2}{2}", DateTime.Now, ex, Environment.NewLine)); 

 

            } 

            else 

            { 

throw new Exception("Could not find section 'ErrorLoggerSection' in the application configuration file."); 

            } 

        } 

    } 

 

    // Inherit from "ConfigurationSection" simple enough! 

    public class ErrorLoggerSection : ConfigurationSection 

    { 

        // I only want one property LogFilePath, and I'm 

        // here telling .NET to look for an attribute in the 

        // config file called 'logFilePath' to get the value from. 

        [ConfigurationProperty("logFilePath", IsRequired = true)] 

        public string LogFilePath 

        { 

            get 

            { 

                return (string)base["logFilePath"]; 

            } 

 

            set 

            { 

                base["logFilePath"] = value; 

            } 

        } 

    } 



Web.config

<?xml version="1.0"?> 

<configuration> 

    <configSections> 

        <section name="WebSiteErrorLogger" type="SingingEels.ErrorLoggerSection"/> 

    </configSections> 

    <WebSiteErrorLogger logFilePath="~/Log/"/> 

 

    <system.web> 

        <compilation debug="true"/> 

    </system.web> 

 

<system.web> 

<customErrors mode="RemoteOnly" defaultRedirect="~/Error.htm"></customErrors> 

</system.web> 

 

</configuration> 

Global.asax

<%@ Application Language="C#" %> 

 

<script runat="server"> 

 

void Application_Start(object sender, EventArgs e) 



// Code that runs on application startup 

 



 

void Application_End(object sender, EventArgs e) 



// Code that runs on application shutdown 

 



 

void Application_Error(object sender, EventArgs e) 



// Code that runs when an unhandled error occurs 

 

Exception err = Server.GetLastError().GetBaseException(); 

SingingEels.ErrorLogger.LogException(err); 

 

 

 



 

void Session_Start(object sender, EventArgs e) 



// Code that runs when a new session is started 

 



 

void Session_End(object sender, EventArgs e) 



// Code that runs when a session ends. 

// Note: The Session_End event is raised only when the sessionstate mode 

// is set to InProc in the Web.config file. If session mode is set to StateServer 

// or SQLServer, the event is not raised. 

 



 

</script>

 

 原始碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.