Asp.Net Core MVC 捕获应用异常细节
Asp.Net Core MVC capture application exception details
经过一些研究后,我找不到在 asp.net 核心 mvc 中捕获应用程序异常并保留默认错误页面行为的方法。实际上有两种自定义处理应用程序错误的方法。第一种简单的方法是在 Startup.cs 文件中配置 app.UseExceptionHandler("/Home/Error");
,但这样我就失去了默认的 DEVELOPMENT 错误页面漂亮视图。 asp.net 核心 mvc 中自定义错误处理的其他解决方案是内联定义异常处理程序,但这也会导致默认错误页面被覆盖:
app.UseExceptionHandler(
options => {
options.Run(
async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
await context.Response.WriteAsync(err).ConfigureAwait(false);
}
});
}
);
我只需要捕获错误详细信息,无需覆盖 默认行为(非常默认的错误页面等)。我不需要任何自定义异常处理程序,事实上我只需要获取异常。我想在应用程序级别执行此操作,因此实现 IExceptionFilter
的自定义 ExceptionHandlerAttribute
将不起作用。该解决方案将删除默认错误页面,我还需要捕获中间件错误,而不仅仅是控制器异常。以下方法不适用:
public class CustomExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
HttpStatusCode status = HttpStatusCode.InternalServerError;
String message = String.Empty;
var exceptionType = context.Exception.GetType();
if (exceptionType == typeof(UnauthorizedAccessException))
{
message = "Unauthorized Access";
status = HttpStatusCode.Unauthorized;
}
else if (exceptionType == typeof(NotImplementedException))
{
message = "A server error occurred.";
status = HttpStatusCode.NotImplemented;
}
else if (exceptionType == typeof(MyAppException))
{
message = context.Exception.ToString();
status = HttpStatusCode.InternalServerError;
}
else
{
message = context.Exception.Message;
status = HttpStatusCode.NotFound;
}
HttpResponse response = context.HttpContext.Response;
response.StatusCode = (int)status;
response.ContentType = "application/json";
var err = message + " " + context.Exception.StackTrace;
response.WriteAsync(err);
}
}
这是我想保留的页面:
解决方案是将 Elm 用于 ASP.NET 核心应用程序,示例代码由 Microsoft 在其 GitHub 帐户:https://github.com/aspnet/Diagnostics, also there is reworked, stable version of the ASP.NET Core MVC logger, described in my article https://www.codeproject.com/Articles/1164750/Error-logging-in-ASP-NET-Core-MVC-Elmah-for-Net-Co 上提供。编码愉快!
经过一些研究后,我找不到在 asp.net 核心 mvc 中捕获应用程序异常并保留默认错误页面行为的方法。实际上有两种自定义处理应用程序错误的方法。第一种简单的方法是在 Startup.cs 文件中配置 app.UseExceptionHandler("/Home/Error");
,但这样我就失去了默认的 DEVELOPMENT 错误页面漂亮视图。 asp.net 核心 mvc 中自定义错误处理的其他解决方案是内联定义异常处理程序,但这也会导致默认错误页面被覆盖:
app.UseExceptionHandler(
options => {
options.Run(
async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
await context.Response.WriteAsync(err).ConfigureAwait(false);
}
});
}
);
我只需要捕获错误详细信息,无需覆盖 默认行为(非常默认的错误页面等)。我不需要任何自定义异常处理程序,事实上我只需要获取异常。我想在应用程序级别执行此操作,因此实现 IExceptionFilter
的自定义 ExceptionHandlerAttribute
将不起作用。该解决方案将删除默认错误页面,我还需要捕获中间件错误,而不仅仅是控制器异常。以下方法不适用:
public class CustomExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
HttpStatusCode status = HttpStatusCode.InternalServerError;
String message = String.Empty;
var exceptionType = context.Exception.GetType();
if (exceptionType == typeof(UnauthorizedAccessException))
{
message = "Unauthorized Access";
status = HttpStatusCode.Unauthorized;
}
else if (exceptionType == typeof(NotImplementedException))
{
message = "A server error occurred.";
status = HttpStatusCode.NotImplemented;
}
else if (exceptionType == typeof(MyAppException))
{
message = context.Exception.ToString();
status = HttpStatusCode.InternalServerError;
}
else
{
message = context.Exception.Message;
status = HttpStatusCode.NotFound;
}
HttpResponse response = context.HttpContext.Response;
response.StatusCode = (int)status;
response.ContentType = "application/json";
var err = message + " " + context.Exception.StackTrace;
response.WriteAsync(err);
}
}
这是我想保留的页面:
解决方案是将 Elm 用于 ASP.NET 核心应用程序,示例代码由 Microsoft 在其 GitHub 帐户:https://github.com/aspnet/Diagnostics, also there is reworked, stable version of the ASP.NET Core MVC logger, described in my article https://www.codeproject.com/Articles/1164750/Error-logging-in-ASP-NET-Core-MVC-Elmah-for-Net-Co 上提供。编码愉快!