I have created a OwinMiddleware to catch all unhandled exceptions on my api requests.
public class UnhandledExceptionMiddleware : OwinMiddleware
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public UnhandledExceptionMiddleware(OwinMiddleware next) : base(next)
{ }
public override async Task Invoke(IOwinContext context)
{
try
{
await Next.Invoke(context);
}
catch (Exception ex)
{
logger.Fatal(ex, ex.Message);
}
}
}
But if I throw some error in my controller function the catch block is not used and in this middleware seems that everything is fine. Why my catch block is not used?
UPDATE
Here my Owin Config. The Invoke Method works only the catch is not working
public void Configuration(IAppBuilder app)
{
// Enables use of spatial data types
SqlServerTypes.Utilities.LoadNativeAssemblies(HostingEnvironment.MapPath("~/bin"));
HttpConfiguration config = new HttpConfiguration();
IContainer container = AutoFacConfig.Register(config, app);
app.UseApplicationInsights();
app.UseNLog();
app.Use<UnhandledExceptionMiddleware>();