2014-09-04 171 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