Web API ExceptionLogger
The only way I have found is to parse ExceptionLoggerContext.ExceptionContext
in your implementation ExceptionLogger
/ IExceptionLogger
.
ExceptionLoggerContext.ExceptionContext
has a type System.Web.Http.ExceptionHandling.ExceptionContext
.
There are several nested objects that you must move around, but at some point you will find:
((System.Web.Http.ApiController)exceptionContext.ControllerContext.Controller).ActionContext.ActionArguments
which contains ValueCollection
with all your arguments FORM
.
In my solution, I have implemented something like this:
public class MyExceptionContext
{
public Dictionary<string, object>.ValueCollection ActionArguments { get; set; }
public MyExceptionContext(System.Web.Http.ExceptionHandling.ExceptionContext exceptionContext)
{
try
{
this.ActionArguments = ((System.Web.Http.ApiController)exceptionContext.ControllerContext.Controller).ActionContext.ActionArguments.Values;
}
catch (Exception)
{
}
}
}
I am passing in my constructor ExceptionLoggerContext.ExceptionContext
; it will fill the dictionary with ActionArguments
all published values.
You can find a demo project on Github. It is a standalone application (owin) web.api. In my Startup, I have defined my custom implementation IExceptionLogger
and IExceptionHandler
.
In this sample application, I used Serilog to stream an on-disk exception.
If you try to submit the form to the test controller, I will throw an exception that will be caught by the global handler and saved to the filesystem with some information.
If you download and run the example found on Github, you can use Fiddler to test it:
POST http://localhost:9667/api/exception HTTP/1.1
User-Agent: Fiddler
Content-Type: application/x-www-form-urlencoded
Host: localhost:9667
Content-Length: 40
username=stackoverflow&password=password
source to share