简体   繁体   English

全局捕获程序异常错误并写入asp.net Core 2.1 Web API中的文件

[英]Globally capture the program exception error and write to a file in asp.net core 2.1 web api

I, am trying to create a global exception handling service for execution error not a http status error in asp.net core 2.1 web api and write to a file. 我正在尝试为asp.net core 2.1 Web API中的执行错误而不是http状态错误创建全局异常处理服务,并写入文件。

Tried few options like 尝试了一些选择,例如

services.AddMvc(config =>
            {
                config.Filters.Add(typeof(GlobalExceptionFilter));
            })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddSessionStateTempDataProvider();

Exception Class 例外类别

public class GlobalExceptionFilter : 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;
            }
            context.ExceptionHandled = true;

            HttpResponse response = context.HttpContext.Response;
            response.StatusCode = (int)status;
            response.ContentType = "application/json";
            var err = message + " " + context.Exception.StackTrace;
            response.WriteAsync(err);
        }
    }

But it never coming to the errors.I tried with debugging but never hit the debugging code. 但是它从来没有出现错误。我尝试调试但从未触及调试代码。

can anyone please let me how to log the global error on file. 任何人都可以让我如何记录文件中的全局错误。

found some of the solution from this solutions https://blog.elmah.io/error-logging-middleware-in-aspnetcore/ 从此解决方案中找到了一些解决方案https://blog.elmah.io/error-logging-middleware-in-aspnetcore/

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-2.1 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/error-handling?view=aspnetcore-2.1

http://www.talkingdotnet.com/global-exception-handling-in-aspnet-core-webapi/ http://www.talkingdotnet.com/global-exception-handling-in-aspnet-core-webapi/

ASP.NET Core Web API exception handling ASP.NET Core Web API异常处理

Have you tried serilog? 您尝试过serilog吗? I use serilog with MongoDb sink . 我将serilog与MongoDb sink一起使用。

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

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