42

I spawn a thread on Application_Start and would like to log exceptions. There is no Context/HttpContext/HttpContext.Current, so how might I get it to log?

At the moment, it does not catch any exception in my threads and if I write ErrorSignal.FromCurrentContext().Raise(ex); I get an error about context cannot be null.

Maybe I can create a dummy HttpContext but somehow I don't think that will work well.

-edit- I tried ErrorSignal.Get(new HttpApplication()).Raise(ex); and it doesn't seem to pick up that exception.

scott-pascoe
  • 1,394
  • 1
  • 11
  • 31

4 Answers4

73

Make sure you set your application name in web.config

<errorLog type="Elmah.SqlErrorLog, Elmah" 
          connectionStringName="nibWeb" 
          applicationName="Nib.Services" />

and then

ErrorLog.GetDefault(null).Log(new Error(error));

will work

scott-pascoe
  • 1,394
  • 1
  • 11
  • 31
Brendan Carey
  • 746
  • 6
  • 2
  • 1
    For what it's worth, if you're using Elmah's in-memory logging, you don't need to make the configuration change, just call ErrorLog.GetDefault(null).Log(new Error(error)); – chris.house.00 Aug 22 '13 at 17:38
  • 4
    WARNING: this approach does NOT seem to raise a true Elmah Error event -- for instance, you will not receive an error email (if you have that configured). You need to call .Raise() for that. Unfortunately, you need to have an HttpContext for .Raise(), so the only way I know to do that in these sort of situations is to store a HttpContext and use it later. – JoeCool Aug 12 '14 at 14:13
2

I wasn't using <errorLog> as in Brendan Carey's answer because I was only in-memory logging. Nevertheless, his command worked great in my case without naming the application:

Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The application has done something.")));

I DID have to recompile Elmah with .NET 4.0, because of an error about needing System.Web.Abstractions 3.5.0.0. My compiled-for-.NET 4.0 fork is here if anyone wants it (also strong naming):

http://code.google.com/r/scottstafford-elmah/

Scott Stafford
  • 40,202
  • 22
  • 116
  • 163
1

For my application, I saved this.Context.ApplicationInstance in Application_Start so that I can call Elmah.ErrorSignal.Get with the saved instance. With the ErrorSignal, I could then Raise. This goes through all the email filters.

Below is the code. I use FluentScheduler to

public class Global : HttpApplication {
    void Application_Start(object sender, EventArgs e) {

        var application = Context.ApplicationInstance;
        FluentScheduler.TaskManager.UnobservedTaskException +=
            (FluentScheduler.Model.TaskExceptionInformation i, UnhandledExceptionEventArgs a) =>
                Elmah.ErrorSignal.Get(application).Raise(i.Task.Exception);

    }
}
John Tseng
  • 5,957
  • 2
  • 23
  • 33
0

I added a solution to: Using ELMAH in a console application that adds ability to send email, tweets and filter in addition to logging.

Community
  • 1
  • 1
Brian Chance
  • 1,229
  • 10
  • 15