ELMAH in a class library, does the web UI consumer need ELMAH too?

1k views Asked by At

I have an N-Tier project, one of the projects being an Error Logging class library. To this I have added the Nuget package for Elmah. Essentially what I am doing is taking additional information from the method that threw the exception, packaging that up inside a new exception that I create, and give that to ELMAH.

The class library can be added to other projects in the solution (Service Layer, UI). When there is no HTTP context I write out to a file at the moment.

string details = BuildExceptionString(exception, extraDetails, arguments);
LoggingModuleException ex = new LoggingModuleException(details);

// We may have come from a website, or a backend layer. Handle both.
if (HttpContext.Current != null)
{
    ErrorSignal.FromCurrentContext().Raise(ex); // ELMAH Signaling 
}
else
{
    // Not a web app so no httpcontext
    System.IO.File.WriteAllText(@"C:\MyLogs\Log.txt", ex.Message);
}

My question is when I call this library code from the UI (MVC) layer I do have a HTTP Context and it calls ELMAH. However I don't know where the errors are going! Does my UI layer need to have ELMAH for the call to Raise() to work?

When I added ELMAH to my class library I did not have any .config file created. I have added an App.config with some ELMAH settings I copied in from another question. I've pasted it below but I can't say that it is being used by the class library at all...

<?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"/>
  <section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
</configSections>
<elmah>
<security allowRemoteAccess="yes" />
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\MyLogs\Elmah\" />
</elmah>
<system.web>
<httpModules>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
</httpModules>

<httpHandlers>
  <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
</system.web>
<location path="elmah.axd">
<system.web>
  <authorization>
    <deny users="?"/>
  </authorization>
</system.web>
</location>
</configuration>
1

There are 1 answers

0
VictorySaber On BEST ANSWER

I have found the answer.

The clue was in the fact that if you need some appSettings, a class library does NOT use its own App.config and instead it pulls them from the caller's .config. So for a MVC site calling this class library, the appSettings should be in web.config.

So in order for the class library, which has ELMAH installed, to log with ELMAH when we have a http context (have come from a website) the website's web.config needs the ELMAH configuration information, which the class library will read & use.

<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>

//...

<system.web>
//...
<httpModules>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
  <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>

//...

</runtime>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<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>
<elmah>
<security allowRemoteAccess="false" />
</elmah>
<location path="elmah.axd" inheritInChildApplications="false">
<system.web>
  <httpHandlers>
    <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  </httpHandlers>
</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>