I just started using ELMAH and am a fan. My team supports a large number of web applications and I'm particularly excited that ELMAH lets us save exceptions from each application to the same MS SQL database table.
We also support a few console, DLL and desktop applications. Is it possible to use the ELMAH DLL to log exceptions in these apps to that same location?
For those that need Brian Chance's answer ported to VB.NET:
However, for just logging errors to the database, this will be sufficient:
As a complete solution:
And
will work if it is called after Initialize()
ELMAH stands for Error Logging Modules and Handlers - referring, of course, to
IHttpModule
andIHttpHandler
.Console applications do not use HTTP, so would typically not be able to benefit much from Modules and Handlers built for HTTP.
Edit: This CAN be done - See this answer.
I'm pretty sure you can't do this. I'll try and dig up the relevant material.
http://groups.google.com/group/elmah/browse_thread/thread/f214c4f782dc2bf4/d96fe43b60765f0c?lnk=gst&q=app#d96fe43b60765f0c
So from what I can find searching the Google group is that it's not possible... Since ELMAH works off of HttpHandlers (an asp.net construct) it is ASP.NET only.
With that said, there are ways that you could utilize it on a console application. ELMAH provides a method to raise errors, so you could wrap ELMAH in your exception handling and then signal an error via:
This would mean wrapping your entire application in an exception handler and signaling. It might take you some tweaking to get it down, but I think it's totally possible.
In case you require it, this is the link to the ELMAH code repository.
We needed the ability to log from a console app and a windows service in addition to our ASP.NET site. I used the answer (
ErrorLog.GetDefault(null);
) which worked well until I needed to email too.So, here is my solution. It handles the log, email, tweet and filtering (both in the config file and in code). I have also wrapped the main call as an extension to Exception so it can be called like:
catch(Exception ex) { ex.LogToElmah(); }
To filter in code, hook the corresponding .Filtering event:
ElmahExtension.ErrorLog.Filtering += new ExceptionFilterEventHandler(ErrorLog_Filtering);
Code:
In addition, you will need to add a reference to the
System.Web.dll
in your project for this to work.EDIT: As per the comments, this code will send emails only if your config file has
<errorMail async="false"/>
. Refer to this code snippet should you want to keep<errorMail async="true"/>
in your config file (to be used when HttpContext.Current is available).Well, since I can't comment I'll post this down here and maybe someone will see it.
After following Brian's method and the commentors I was able to get email working but I still wasn't seeing the SQL messages being logged, even though I had set the applicationName. What I didn't realize is that they were actually being logged I just wasn't seeing them because the applicationName must be the same as your web.config in order to be able to view it.
My web.config didn't have applicationName specified, so it was defaulting to "/LM/W3SVC/2/ROOT", which is basically what "asgeo1" commented, though I didn't realize it had to be the same.
Since I didn't really have any errors I was concerned with, I configured applicationName in my web.config and my app.config to be the same and now everything shows up like a champ.
We have exactly the same situation here. Running ELMAH for all our web applications. A few of them have console based schedulers.
After doing some digging through the source code, the following code seems to work:
The only real problem with the above is that you need to keep the application name somewhere in your config to be able to see the entries on the ELMAH.axd viewer.
So in our generic error handling code we do: