Elmah Doesn't Email Fire Report and Forgets

I have an MVC application where I am trying to capture all incoming requests in an ActionFilter. Here is the registration code. I'm trying to get into the fire and forget the model.

My problem is that I execute this code synchronously, taking out Task.Run Elmah sends an email. But for the code shown below, I see an error being logged in the InMemory log in elmah.axd, but not with email messages.

public void Log(HttpContextBase context) { Task.Run(() => { try { throw new NotImplementedException(); //simulating an error condition using (var s = _documentStore.OpenSession()) { s.Store(GetDataToLog(context)); s.SaveChanges(); } } catch (Exception ex) { ErrorSignal.FromCurrentContext().Raise(ex); } }); } 
+1
source share
1 answer

Got this response from Atif Aziz (Elmah Lead contributor) to google ELMAH

When you use Task.Run, the HttpContext is not transferred to the thread pool thread where your action will be executed. When ErrorSignal.FromCurrentContext is called from your action, I assume that it probably does not work with another exception, because there is no current context. This exception lies in the Problem. If you're on .NET 4, you're in luck because you will end up with an ASP.NET application application (but maybe a lot after the fact) when the GC launches and collects the task and its exception will be “unobservable.” If you are using .NET 4.5, the policy has been changed, and the exception is simply lost. In any case, your observation will be that the mailing does not work. In fact, logging will not work if you do not use Elmah.ErrorLog.GetDefault (null ) .Log (new error (ex)), where context zero is allowed, but this call only registers but it doesn’t perform any mailings. ELMAH modules are connected to the ASP.NET context. If you separate from this context by clicking on another thread, you cannot rely on ELMAH modules. You can use Elmah.ErrorLog.GetDefault (null) .Log (new error (ex)) reliably log the error.

  • Atif
+3
source

All Articles