2014-09-04 73 views
0

我在global.asax中創建了自定義的錯誤處理,我處理未處理的異常。 自定義消息被生成並顯示,但ELMAH記錄異常。如何禁用ELMAH未處理的異常日誌記錄?

唯一的問題是,ELMAH記錄加倍。因爲ELMAH unhandledException在全局Application_Error之前處理觸發事件,並在其中再次記錄。

我需要的Application_Error項,因爲這是地方,那裏異常的GUID生成並追加到用戶信息並寫入Exception.Data,在那裏它被記錄ELMAH(我延長記錄儀)

如何跳過ELMAH未處理的異常日誌記錄?

+0

你不能刪除'ErrorLog','ErrorMail'和Web.config中的'ErrorFilter'模塊,如果你只是打算使用它手動? – CodingIntrigue 2014-09-04 07:22:59

+0

然後沒有被記錄。 – berzinsu 2014-09-04 07:28:36

+0

本答題中的答案是否可以幫助您提供一些指導,幫助您如何繼續? - http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute – Tommy 2014-09-17 01:41:31

回答

1

轉到web.config system.webServer/modules部分並評論/刪除elmah http模塊。這將阻止elmah從未處理的異常記錄。

<!--<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />--> 

登錄所有的錯誤到ELMAH在Global.asax中手動:

protected void Application_Error() 
{ 
    var lastException = Server.GetLastError(); 
    var customException = new Exception("custom message", lastException); 
    Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(customException)); 
} 

完整的web.config例如:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 

<configuration> 
    <configSections> 
    <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> 
    </configSections> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="PreserveLoginUrl" value="true" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 

    <system.web> 

    <httpRuntime targetFramework="4.5" /> 

    <compilation debug="true" targetFramework="4.5" /> 

    <pages> 
     <namespaces> 
     <add namespace="System.Web.Helpers" /> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Routing" /> 
     <add namespace="System.Web.WebPages" /> 
     </namespaces> 
    </pages> 
    <httpModules> 
     <!--<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />--> 
     <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" /> 
     <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> 
    </httpModules> 
    </system.web> 

    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 

    <handlers> 
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> 
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 
    <modules> 
     <!--<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />--> 
     <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" /> 
     <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /> 
    </modules> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <elmah> 
    <!-- 
     See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
     more information on remote access and securing ELMAH. 
    --> 
    <security allowRemoteAccess="false" /> 
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" /> 
    </elmah> 
    <location path="elmah.axd" inheritInChildApplications="false"> 
    <system.web> 
     <httpHandlers> 
     <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> 
     </httpHandlers> 
     <!-- 
     See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
     more information on using ASP.NET authorization securing ELMAH. 

     <authorization> 
     <allow roles="admin" /> 
     <deny users="*" /> 
     </authorization> 
     --> 
    </system.web> 
    <system.webServer> 
     <handlers> 
     <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> 
     </handlers> 
    </system.webServer> 
    </location> 
    <connectionStrings> 
    <add name="elmah-sqlserver" connectionString="Data Source=.\SQLEXPRESS2012;Initial Catalog=Elmah;Integrated Security=True" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
</configuration> 
+0

如果部分被取出,則日誌配置將變爲無效 '' – berzinsu 2014-09-23 07:54:44

+0

@berzinsu ups抱歉,我沒有注意到您正在使用sql服務器配置 – 2014-09-23 07:59:31

+0

@berzinsu bocomes無效的意思是什麼?對我來說,elmah仍然工作後,我刪除了http模塊。我也登錄到SQL。 – 2014-09-23 08:09:27