Web Api - 异常未记录
Web Api - Exception not logging
我有以下class
public class WebApiExceptionLoggingFilter : ExceptionFilterAttribute
{
private readonly ILog _logger;
public WebApiExceptionLoggingFilter(ILog logger)
{
this._logger = logger;
}
public override void OnException(HttpActionExecutedContext filterContext)
{
//Logging Exception
_logger.Error("An unhandled exception was thrown", filterContext.Exception);
//Changing exception message to something generic.
var exceptionMessage = "An error occurred and logged during processing of this application.";
//Throwing a proper message for the client side
var message = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(exceptionMessage),
ReasonPhrase = exceptionMessage
};
throw new HttpResponseException(message);
}
}
我正在注册它:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Filters.Add(new WebApiExceptionLoggingFilter(LogManager.GetLogger("Web Api Unhandled Exception Logger")));
}
我为我的 mvc 控制器注册了另一个过滤器属性,它已在 FilterConfig.cs 文件中注册。当控制器操作中发生异常时,它会通过 log4net 记录到 windows 事件查看器。当在调用 Web api 控制器操作时发生异常时,将命中上述处理程序中的 OnException 方法并调用 _logger.log,但事件查看器不会记录任何内容。我错过了什么吗?
终于找到问题了
我的 assemblyinfo.cs
里有这个
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
我需要这个
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",Watch = true)]
我有以下class
public class WebApiExceptionLoggingFilter : ExceptionFilterAttribute
{
private readonly ILog _logger;
public WebApiExceptionLoggingFilter(ILog logger)
{
this._logger = logger;
}
public override void OnException(HttpActionExecutedContext filterContext)
{
//Logging Exception
_logger.Error("An unhandled exception was thrown", filterContext.Exception);
//Changing exception message to something generic.
var exceptionMessage = "An error occurred and logged during processing of this application.";
//Throwing a proper message for the client side
var message = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(exceptionMessage),
ReasonPhrase = exceptionMessage
};
throw new HttpResponseException(message);
}
}
我正在注册它:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Filters.Add(new WebApiExceptionLoggingFilter(LogManager.GetLogger("Web Api Unhandled Exception Logger")));
}
我为我的 mvc 控制器注册了另一个过滤器属性,它已在 FilterConfig.cs 文件中注册。当控制器操作中发生异常时,它会通过 log4net 记录到 windows 事件查看器。当在调用 Web api 控制器操作时发生异常时,将命中上述处理程序中的 OnException 方法并调用 _logger.log,但事件查看器不会记录任何内容。我错过了什么吗?
终于找到问题了
我的 assemblyinfo.cs
里有这个[assembly: log4net.Config.XmlConfigurator(Watch = true)]
我需要这个
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",Watch = true)]