如何在Asp.Net Core中自定义开发人员例外页面? [英] How to customize Developer Exception Page in Asp.Net Core?

查看:90
本文介绍了如何在Asp.Net Core中自定义开发人员例外页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Startup.cs文件的Configure方法中,通常具有如下所示的代码:

It's common in the Configure method of the Startup.cs file to have code that looks like this:

if(env.IsDevelopment()) {
     app.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions() { SourceCodeLineCount = 10 });
} 

app.UseDeveloperExceptionPage();行会在发生异常时在浏览器中显示详细信息,这对于调试非常有帮助.

The app.UseDeveloperExceptionPage(); line causes detailed information to be displayed in the browser when an exception occurs which is quite helpful for debugging.

我确定您已经看到了输出,它看起来像这样:

I'm sure you've seen the output, it looks something like this:

这是一个很好的开始,但是有时作为开发人员,我们向异常添加其他信息以提供有关错误的详细信息. System.Exception类具有一个Data集合,可用于存储此类信息.因此,例如,从Exception继承的我的自定义AppException具有一个构造器,该构造器除了接受标准异常消息外还接受私有消息,如下所示:

This is a great start, but sometimes as developers we add additional information to exceptions to provide details about the error. The System.Exception class has a Data collection that can be used for storing such information. So for example my custom AppException that inherits from Exception has a constructor that accepts a private message in addition to the standard exception message, like so:

 /// <summary>
 /// Application Exception. 
 /// </summary>
 /// <param name="message">Message that can be displayed to a visitor.</param>
 /// <param name="privateMessage">Message for developers to pinpoint the circumstances that caused exception.</param>
 /// <param name="innerException"></param>
    public AppException(string message, string privateMessage, Exception innerException = null) 
        : base(message, innerException) {
        
        //Placing it in the exception Data collection makes the info available in the debugger
        this.Data["PrivateMessage"] = privateMessage;
    }

因此,您可以想象,如果开发人员例外页面显示了Exception中的PrivateMessage(如果它是AppException)以及所有其他信息,那将是很好的选择.我想尽办法找出如何自定义或扩充开发人员例外页面上显示的信息,但是却找不到任何好的信息.

So as you can imaging, it'd be nice if the developer exception page displayed the PrivateMessage from the Exception (if it's an AppException) along with all the other information. I've looked high and low to figure out how to customize or augment the information displayed on the developer exception page but haven't been able to find any good information on it.

如何自定义或扩充开发人员例外页面上显示的信息?

推荐答案

app.UseDeveloperExceptionPage()扩展方法是将DeveloperExceptionPageMiddleware添加到请求/响应管道中的简写.您可以找到DeveloperExceptionPageMiddleware的完整源代码此处,包括中间件本身和视图.它应该为滚动您自己的自定义中间件奠定良好的基础.

The app.UseDeveloperExceptionPage() extension method is shorthand for adding the DeveloperExceptionPageMiddleware into the request/response pipeline. You can find the full source code for DeveloperExceptionPageMiddleware here, including the middleware itself and the views. It should make an excellent base for rolling your own custom middleware.

这篇关于如何在Asp.Net Core中自定义开发人员例外页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆