ASP.NET的WebAPI IExceptionLogger没有捕获异常, [英] ASP.NET WebApi IExceptionLogger doesn't catch exceptions
问题描述
我想设置为在这里提出了一个全球性的异常处理程序:<一href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling\">Web API全局错误处理。我已经设置,其中一个异常被一个控制器构造函数中抛出的情况,但我的例外是没有得到记录,相反的WebAPI刚刚返回例外,全堆栈跟踪到调用客户端作为JSON消息。
我不知道它的问题,但我的控制器动作是用异步/等待是这样的:
[HTTPGET,路线(GetLocationNames)]
公共异步任务&LT;&IEnumerable的LT;弦乐&GT;&GT;得到()
{
返回等待adapter.GetLocationNames();
}
我有以下实现:
使用log4net的;
使用System.Threading.Tasks;
使用System.Web.Http.ExceptionHandling;命名空间warehouse.management.api
{
公共类Log4NetExceptionLogger:ExceptionLogger
{
私人ILog的日志= LogManager.GetLogger(typeof运算(Log4NetExceptionLogger)); 公共异步替代任务LogAsync(ExceptionLoggerContext背景下,System.Threading.CancellationToken的CancellationToken)
{
log.Error(发生未处理的异常。context.Exception);
等待base.LogAsync(背景下,的CancellationToken);
} 公共覆盖无效日志(ExceptionLoggerContext上下文)
{
log.Error(发生未处理的异常。context.Exception);
base.Log(上下文);
} 公众覆盖布尔ShouldLog(ExceptionLoggerContext上下文)
{
返回base.ShouldLog(上下文);
}
}
}
和我注册这样的:
使用Owin;
使用System.Web.Http;
使用System.Web.Http.ExceptionHandling;命名空间warehouse.management.api.Config
{
公共静态类WebApiConfig
{ 公共静态IAppBuilder RegisterApiConfig(这IAppBuilder应用程序,HttpConfiguration配置)
{
config.MapHttpAttributeRoutes();
config.Services.Add(typeof运算(IExceptionLogger),新Log4NetExceptionLogger());
返回程序;
}
}
}
这是我的packages.conf:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;套餐及GT;
&LT;包ID =log4net的版本=2.0.3targetFramework =net45/&GT;
&LT;包ID =Microsoft.AspNet.WebApi.Client版本=5.1.2targetFramework =net45/&GT;
&LT;包ID =Microsoft.AspNet.WebApi.Core版本=5.1.2targetFramework =net45/&GT;
&LT;包ID =Microsoft.AspNet.WebApi.Owin版本=5.1.2targetFramework =net45/&GT;
&LT;包ID =Microsoft.AspNet.WebApi.WebHost版本=4.0.20505.0targetFramework =net45/&GT;
&LT;包ID =Microsoft.Owin版本=2.1.0targetFramework =net45/&GT;
&LT;包ID =Microsoft.Web.Infrastructure版本=1.0.0.0targetFramework =net45/&GT;
&LT;包ID =Newtonsoft.Json版本=4.5.11targetFramework =net45/&GT;
&LT;包ID =Owin版本=1.0targetFramework =net45/&GT;
&LT;包ID =团结版本=3.5.1404.0targetFramework =net45/&GT;
&LT;包ID =Unity.AspNet.WebApi版本=3.5.1404.0targetFramework =net45/&GT;
&LT;包ID =WebActivatorEx版本=2.0targetFramework =net45/&GT;
&LT; /包&GT;
原来这是排序。在我的启动类(未显示)我搬到调用我的RegisterApiConfig方法(上面,在OP)我呼吁UseWebApi之前和现在的作品。
I'm trying to setup a global exception handler as outlined here: Web API Global Error Handling. I"ve setup a case where an exception gets thrown within a controller constructor. But my exception isn't getting logged. Instead WebApi is just returning the exception and full stack trace to the calling client as a JSON message.
I don't know if it matters, but my controllers actions are using async / await like this:
[HttpGet, Route("GetLocationNames")]
public async Task<IEnumerable<String>> Get()
{
return await adapter.GetLocationNames();
}
I have the following implementation:
using log4net;
using System.Threading.Tasks;
using System.Web.Http.ExceptionHandling;
namespace warehouse.management.api
{
public class Log4NetExceptionLogger : ExceptionLogger
{
private ILog log = LogManager.GetLogger(typeof(Log4NetExceptionLogger));
public async override Task LogAsync(ExceptionLoggerContext context, System.Threading.CancellationToken cancellationToken)
{
log.Error("An unhandled exception occurred.", context.Exception);
await base.LogAsync(context, cancellationToken);
}
public override void Log(ExceptionLoggerContext context)
{
log.Error("An unhandled exception occurred.", context.Exception);
base.Log(context);
}
public override bool ShouldLog(ExceptionLoggerContext context)
{
return base.ShouldLog(context);
}
}
}
And I'm registering it like this:
using Owin;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
namespace warehouse.management.api.Config
{
public static class WebApiConfig
{
public static IAppBuilder RegisterApiConfig(this IAppBuilder app, HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Services.Add(typeof(IExceptionLogger), new Log4NetExceptionLogger());
return app;
}
}
}
And here's my packages.conf:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20505.0" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
<package id="Unity" version="3.5.1404.0" targetFramework="net45" />
<package id="Unity.AspNet.WebApi" version="3.5.1404.0" targetFramework="net45" />
<package id="WebActivatorEx" version="2.0" targetFramework="net45" />
</packages>
Turns out it was the ordering. In my startup class (not shown) I moved the call to my RegisterApiConfig method (above, in the OP) to before my call to UseWebApi and now it works.
这篇关于ASP.NET的WebAPI IExceptionLogger没有捕获异常,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!