ELMAH not logging errors in MVC 2 app
I'm using the suggestion from this question: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?
I used Aziz's second code sample.
I ran the debugger to see what happens when an error occurs. The main function in the HandleErrorAttribute class is:
public override void OnException(ExceptionContext context)
{
base.OnException(context);
var e = context.Exception;
if (!context.ExceptionHandled // if unhandled, will be logged anyhow
|| RaiseErrorSignal(e) // prefer signaling, if possible
|| IsFiltered(context)) // filtered?
return;
LogException(e);
}
It reaches the if statement. context.ExceptionHandled
returns true, RaiseErrorSignal(e)
also returns true. IsFiltered is never reached, it just returns, and LogException(e)
is never reached.
Has anyone run into this?
EDIT: Adding web.config
<?xml version="1.0" encoding="utf-8"?>
<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>
<elmah>
<security allowRemoteAc开发者_如何学编程cess="0" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="AppDb" />
</elmah>
<appSettings></appSettings>
<connectionStrings>
<add name="AppDb" connectionString="metadata=res://*/Models.MyDB.csdl|res://*/Models.MyDB.ssdl|res://*/Models.MyDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
</httpModules>
<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
</modules>
<handlers>
<add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
It looks like your AppDb connection string is an Entity Framework connection string. I don't think ELMAH was written to recognize that format. Try giving ELMAH a standard SQL connection string and see if that works.
Its short circut logic so it will stop at || RaiseErrorSignal(e)
and not evaluate IsFiltered(context)
The fact that context.ExceptionHandled
is true indicates that you have explictly handled the exception yourself in your code with a try catch block. In this case you need to explicitly tell ELMAH to handle the error as well:
catch (Exception ex)
{
// do something
// etc
// now tell ELMAH to handle it too
ErrorSignal.FromCurrentContext().Raise(ex);
}
The call to RaiseErrorSignal(e)
will record the error within ELMAH, LogException(e)
does not need to be called if RaiseErrorSignal(e)
returns true.
If ELMAH is properly configured both RaiseErrorSignal(e)
and LogException(e)
will result in an error being recorded. There are a few reasons for which RaiseErrorSignal(e)
could return false, so LogException(e)
exists merely as a backup should signaling fail.
If errors are still not showing up in ELMAH then chances are you have a problem with your configuration. Could you please post the relevant parts of your Web.config so we can have a look? Namely, please post the <elmah />
section and also your http modules in <system.web />
or <system.webServer />
(if you're using an IIS7 environment).
Update
As Erik King pointed out- your connection string 'AppDb' is for Entity Framework. It will not work as an ErrorLog connection string. If you add a standard connection string to your Web.config and point the ErrorLog at it, ELMAH should start logging correctly.
In case you were unaware- the ErrorFilter HttpModule must be declared after any modules it should filter. In your current Web.config if you were to define a filter it would only be applied to the ErrorLog module and not the ErrorMail module.
精彩评论