Search code examples
asp.netiiselmah

Using ELMAH with ASP.NET WebApp with a Class library


I have an ASP.NET 4.5 Web Application (running on IIS 7.5), which calls a method from a class library I have referenced. I want to add ELMAH to log when I have unhandled exceptions. I have installed ELMAH with NuGet. It works great. The problem is that it doesn't log any exceptions coming from the class library, it only logs the ones coming from the web application.

I tried adding a try catch in my class library and tried to log the exception manually with:

ErrorSignal.FromCurrentContext().Raise(e);

But this doesn't do anything.

Do I have to install and reference ELMAH on both web application and class library ? And will the web.config be enough for the class library to know which handlers and modules to use ? Or do I need to add another config in the class library ?

I noticed that both web app and class library have a packages.config but it only contains:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="elmah" version="1.2.2" targetFramework="net45" />
  <package id="elmah.corelibrary" version="1.2.2" targetFramework="net45" />
</packages>

I have found many pages on stackoverflow about this topic but couldn't find an answer that works for me.

Some people recommended using

ErrorLog.GetDefault(HttpContext).Log(new Error(errExc)); 

But HttpContext is not available. I tried using null instead of HttpContext but it didn't help either.


Solution

  • While searching online, I found out that ELMAH only logs the stuff coming from the web app. I was able to log the stuff coming from the class library by installing NLog or using System.Diagnostic.Trace.

    Perhaps I'm wrong about this, and I'd be open to corrections.