I'm writing an exception filter that will log exceptions to Elmah, so I'm doing something like this:
class ExceptionLoggingFilter : System.Web.Http.Filters.IExceptionFilter
{
public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actExecContext,
CancellationToken cancellationToken)
{
var httpContext = // what do I do here?
var errorSignal = ErrorSignal.FromContext(httpContext); // this is Elmah
errorSignal.Raise(actExecContext.Exception, httpContext);
}
}
My problem is that I don't know what to put in place of the comment question. I've tried to explore the member tree of the HttpActionExecutedContext
that I get from the method signature to find a way to a System.Web.HttpContext
, but I can't find any way to get there.
How do I accomplish my goal here?
Elmah.ErrorSignal.FromCurrentContext()
uses HttpContext.Current
under the hood, so although that might have worked I did find a better (albeit more roundabout) way to get to it:
var request = actionExecutedContext.Request;
object value;
if (request == null
|| !request.Properties.TryGetValue("MS_HttpContext", out value)
|| !(value is HttpContextBase))
return null; // Actually I'm returning a Maybe monad here but that's off topic...
}
var httpContextBase = value as HttpContextBase;
return httpContextBase.ApplicationInstance.Context;
I'm using this together with a maybe monad implementation that lets me use ErrorSignal.FromCurrentContext()
as fallback, and so far it's worked well.