Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 (<code>ErrorLog.GetDefault(null);</code>) which worked well until I needed to email too.</p> <p>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: <code>catch(Exception ex) { ex.LogToElmah(); }</code></p> <p>To filter in code, hook the corresponding .Filtering event: <code>ElmahExtension.ErrorLog.Filtering += new ExceptionFilterEventHandler(ErrorLog_Filtering);</code></p> <p>Code:</p> <pre><code>using System; using System.Web; using Elmah; namespace System { public static class ElmahExtension { public static void LogToElmah(this Exception ex) { if (HttpContext.Current != null) { ErrorSignal.FromCurrentContext().Raise(ex); } else { if (httpApplication == null) InitNoContext(); ErrorSignal.Get(httpApplication).Raise(ex); } } private static HttpApplication httpApplication = null; private static ErrorFilterConsole errorFilter = new ErrorFilterConsole(); public static ErrorMailModule ErrorEmail = new ErrorMailModule(); public static ErrorLogModule ErrorLog = new ErrorLogModule(); public static ErrorTweetModule ErrorTweet = new ErrorTweetModule(); private static void InitNoContext() { httpApplication = new HttpApplication(); errorFilter.Init(httpApplication); (ErrorEmail as IHttpModule).Init(httpApplication); errorFilter.HookFiltering(ErrorEmail); (ErrorLog as IHttpModule).Init(httpApplication); errorFilter.HookFiltering(ErrorLog); (ErrorTweet as IHttpModule).Init(httpApplication); errorFilter.HookFiltering(ErrorTweet); } private class ErrorFilterConsole : ErrorFilterModule { public void HookFiltering(IExceptionFiltering module) { module.Filtering += new ExceptionFilterEventHandler(base.OnErrorModuleFiltering); } } } } </code></pre> <p>In addition, you will need to add a reference to the <code>System.Web.dll</code> in your project for this to work.</p> <p><strong>EDIT</strong>: As per the comments, this code will send emails only if your config file has <code>&lt;errorMail async="false"/&gt;</code>. Refer to <a href="http://pastebin.com/L2xTT71K" rel="noreferrer">this code snippet</a> should you want to keep <code>&lt;errorMail async="true"/&gt;</code> in your config file (to be used when HttpContext.Current is available). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload