C# log4net捕获web api的所有控制器操作错误
我目前正在实现log4net以捕获web api控制器的异常 我想知道我如何可能捕获控制器内任何操作中的所有错误C# log4net捕获web api的所有控制器操作错误,c#,asp.net-web-api,log4net,C#,Asp.net Web Api,Log4net,我目前正在实现log4net以捕获web api控制器的异常 我想知道我如何可能捕获控制器内任何操作中的所有错误 我不希望在理想情况下完成每个操作并添加try-catch(如果有帮助的话)您可以实现System.Web.Http.Filters.ActionFilterAttribute。在OnActionExecuted中,您可以处理日志记录: public override void OnActionExecuted(HttpActionExecutedContext actionE
我不希望在理想情况下完成每个操作并添加try-catch(如果有帮助的话)您可以实现System.Web.Http.Filters.ActionFilterAttribute。在OnActionExecuted中,您可以处理日志记录:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
{
//ok
}
else
{
_logger.ErrorFormat("actionExecutedContext.Response == null will result in 500, unhandled exception"); //Add extra information here
}
}
然后,您可以将此ActionFilter作为属性添加到要记录的方法。您可以实现System.Web.Http.Filters.ActionFilterAttribute。在OnActionExecuted中,您可以处理日志记录:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
{
//ok
}
else
{
_logger.ErrorFormat("actionExecutedContext.Response == null will result in 500, unhandled exception"); //Add extra information here
}
}
然后,您可以将此ActionFilter作为属性添加到要记录的方法中。您需要注册一个
e、 x.:GlobalConfiguration.Configuration.Services.Add(typeof(IEExceptionLogger),newYourWebAPIexceptionLogger()) 您需要注册一个
e、 x.:GlobalConfiguration.Configuration.Services.Add(typeof(IEExceptionLogger),newYourWebAPIexceptionLogger()) 对于未处理的异常-这是关于“ASP.NET Web API”的问题吗?如果是这样,考虑添加[ASP.NETWebAPI]标签。哦,如果是的话,可能是答案。欢呼需要在Web API服务器上捕获异常,以帮助我们在Live系统上有任何问题。不确定跟踪是否适用于此?对于未处理的异常,这是关于“ASP.NET Web API”的问题吗?如果是这样,考虑添加[ASP.NETWebAPI]标签。哦,如果是的话,可能是答案。欢呼需要在Web API服务器上捕获异常,以帮助我们在Live系统上有任何问题。不确定跟踪是否适用于此?