[C#][ASP.NET MVC]ELMAH

  • 12497
  • 0
  • C#
  • 2010-04-09

[C#][ASP.NET MVC]ELMAH

上一篇大概自己實做了Error Handling,但在廣大的網際網路可不能每次都這樣苦幹,

工具運用得宜可說提高不少效率喔!(好工具帶我們上天堂, 壞工具帶我們住套房XD),

所以今天就來使用elmah替我們紀錄整個網站的例外事件,

該工具設定過程簡單明瞭,重點是'資訊豐富',實在不錯用。

 ELMAH On IIS6

1.download elmah並在專案中加入參考

image

2.編輯Web.config呼叫Elmah

<configuration>

<sectionGroup name="elmah">
    <section name="security" requirePermission="false" 
        type="Elmah.SecuritySectionHandler, Elmah" />
    <section name="errorLog" requirePermission="false" 
        type="Elmah.ErrorLogSectionHandler, Elmah" />
    <section name="errorMail" requirePermission="false" 
        type="Elmah.ErrorMailSectionHandler, Elmah" />
    <section name="errorFilter" requirePermission="false" 
        type="Elmah.ErrorFilterSectionHandler, Elmah" />      
</sectionGroup>

 

<httpHandlers> 

<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />

 

 

<httpModules>

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
 

 

 

<system.web>只有有任何列外狀況都將導到Error.aspx

<customErrors mode="On" defaultRedirect="~/Error">
      <error statusCode="404" redirect="~/Error"/>
      <error statusCode="500" redirect="~/Error"/>
</customErrors>
<configuration>導出xml檔案到相關存放路徑
<elmah>
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="D:/Log_Data" />   
</elmah>
 

 

 

3.Configure routing(RegisterRoutes in Global.asax.cs)

 routes.IgnoreRoute( "elmah.axd" );  
 

 

 

到這裡就算完成整個elmah的設定了,現在準備來測試搂。

 

丟出NullException

public ActionResult PersonCreate()
        {
            throw new ArgumentNullException();               
            return View();
        }

 

 

image

查看Log(http://server//elmah.axd)

image

點擊Details(擷取部分)

image

xml檔案

image

image

資訊相當詳細完整。

 

 

參考

Using ELMAH with ASP.NET MVC

ELMAH - Error Loggin Module and Handler For unhandled errors in asp.net

Handling Exceptions in ASP.NET MVC