如何使用ASP.NET Web API设置Elmah [英] How to set up Elmah with ASP.NET Web API
问题描述
我尝试了许多elmah nuget,但它们不适用于ASP.NET Web API。有人知道为什么吗?
I've tried many elmah nugets but they didn't work with ASP.NET Web API. Does anybody knows why? Is there any work around for that?
推荐答案
使用ELMAH在WEB API中捕获异常有两种选择。
There are two options by using ELMAH to capture exceptions in WEB API.
如果要捕获操作和控制器中遇到的错误,即在您的业务逻辑中,您可以创建ActionFilterAttribute并将这些异常记录到ELMAH中。
If you want to capture errors that are encountered in Actions and Controllers , i.e. in your business logic you can create a ActionFilterAttribute and log those exceptions to ELMAH.
示例:
public class UnhandledExceptionFilter : ExceptionFilterAttribute {
public override void OnException(HttpActionExecutedContext context) {
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(context.Exception));
}
}
然后通过添加该过滤器进行接线。
Then wireup by adding that filter.
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Filters.Add(new UnhandledExceptionFilter());
}
}
使用上述方法将不会处理以下错误:
With the above approach following errors will not be handled:
- 从控制器构造函数引发的异常。
- 消息处理程序引发的异常。
- 在路由过程中引发的异常。
- 响应内容序列化过程中引发的异常
- Exceptions thrown from controller constructors.
- Exceptions thrown from message handlers.
- Exceptions thrown during routing.
- Exceptions thrown during response content serialization
参考: http ://blogs.msdn.com/b/webdev/archive/2012/11/16/capturing-unhandled-exceptions-in-asp-net-web-api-s-with-elmah.aspx & http://www.asp.net / web-api / overview / error-handling / web-api-global-error-handling
为了让ELMAH登录 WEB API全局级别的错误,以便捕获所有500个服务器错误,然后执行以下操作:
In order for ELMAH to log WEB API errors at the global level such that all 500 Server errors are caught then do this:
安装nuget: https://www.nuget.org/packages/Elmah.Contrib.WebApi/
将以下内容添加到WebApiConfig
Add the below to WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
...
config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());
...
}
}
如果需要要显示有关500个服务器错误的自定义错误消息,您可以在Web Api中实现新的ExceptionHandler(请注意ExceptionLogger和ExceptionHandler是不同的。)
And if you want to show custom error messages for the 500 Server errors you can implement the new ExceptionHandler in Web Api (Note that ExceptionLogger and ExceptionHandler are different.)
class OopsExceptionHandler : ExceptionHandler
{
public override void HandleCore(ExceptionHandlerContext context)
{
context.Result = new TextPlainErrorResult
{
Request = context.ExceptionContext.Request,
Content = "Oops! Sorry! Something went wrong." +
"Please contact support@contoso.com so we can try to fix it."
};
}
private class TextPlainErrorResult : IHttpActionResult
{
public HttpRequestMessage Request { get; set; }
public string Content { get; set; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage response =
new HttpResponseMessage(HttpStatusCode.InternalServerError);
response.Content = new StringContent(Content);
response.RequestMessage = Request;
return Task.FromResult(response);
}
}
}
参考: http://www.asp.net/web-api / overview / error-handling / web-api-global-error-handling
这篇关于如何使用ASP.NET Web API设置Elmah的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!