简体   繁体   English

如何将用户重定向到错误页面并在.NET Core的日志文件中记录错误

[英]How to redirect user to Error page and log errors in log file in .NET core

I am working on a .NET core web app and my requirement is to handle the errors globally. 我正在开发.NET核心Web应用程序,我的要求是全局处理错误。

So to achieve this behavior I tried to use build in middleware UseExceptionHandler along with ExceptionHandlerOptions . 因此,为了实现此行为,我尝试将中间件UseExceptionHandler中的构建与ExceptionHandlerOptions一起使用。

Startup.cs Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(new ExceptionHandlerOptions {
                    ExceptionHandlingPath = "/Home/Error",
                    ExceptionHandler = async (context) => {
                        var exceptionFeature = context.Features.Get<IExceptionHandlerFeature>();
                        if (exceptionFeature != null)
                        {
                            Log.Error(exceptionFeature.Error, exceptionFeature.Error.Message);
                        }
                    }
                });
            }
     }

Above code log the errors in the log file but never redirect the user to an error page. 上面的代码将错误记录在日志文件中,但是永远不要将用户重定向到错误页面。 When I remove ExceptionHandler function it works fine and redirects the user to the error page. 当我删除ExceptionHandler函数时,它可以正常工作,并将用户重定向到错误页面。 Correct me if I missed something. 如果我错过了什么,请纠正我。

I am expecting user should redirect to error page when any unhandled exception occurs and logs the error in log file. 我希望用户在发生任何未处理的异常时应重定向到错误页面,并将错误记录在日志文件中。

My workaround for this problem: 我针对此问题的解决方法:

Startup.cs Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
        }

HomeController.cs HomeController.cs

public async Task<IActionResult> Error(string errorId)
        {
            var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerFeature>();
            if (exceptionFeature != null)
            {
                Log.Error(exceptionFeature.Error, exceptionFeature.Error.Message);
            }
       }

Above solution works for me but is it good practice to log global error in controller? 上面的解决方案对我有用,但是在控制器中记录全局错误是一种好习惯吗? Any lead would be appriciated. 任何线索将被申请。

Thanks! 谢谢!

The two properties are mutually exclusive, ExceptionHandlingPath = "/Home/Error", ExceptionHandler ie, either one of those will be applied, and that's why the redirection is not happening in home/error. 这两个属性是互斥的,ExceptionHandlingPath =“ / Home / Error”,ExceptionHandler,即将应用其中一个,这就是为什么重定向不会在home / error中发生的原因。 If you will remove the exceptionHandler, it will work for you, however you will need to do logging in the controller, which is part of the 2nd implementation you did, and there is no harm in logging it that way till this controller/action does the handling for everything. 如果要删除exceptionHandler,它将为您工作,但是您需要在控制器中进行日志记录,这是您执行的第二个实现的一部分,并且以这种方式记录日志直到此控制器/操作执行完为止没有危害。处理一切。

when i say everything, it means, there are 401 errors, 404 errors, which will not be handled by the current approach, and for that you should use statusCode base approach. 当我说所有内容时,这意味着存在401错误,404错误,当前的方法将无法处理,为此,您应该使用statusCode基本方法。

the better approach will be that you Have multiple implementation of error pages: 1. 404 : where route don't exists 2. 500 : Application error (all the cases) 3 401 : if there is some kind of authorization involved. 更好的方法是对错误页面进行多种实现:1. 404:不存在路由2. 500:应用程序错误(在所有情况下)3401:是否涉及某种授权。

.net core provides a lot of error handling mechanism (including the Globalfilterattributes for exception handling). .net核心提供了许多错误处理机制(包括用于异常处理的Globalfilter属性)。 However if you have a public facing application, you should target to show friendly error page to user and log rest. 但是,如果您有一个面向公众的应用程序,则应针对向用户显示友好的错误页面并记录休息。

You can read about it here 你可以在这里阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM