代码之家  ›  专栏  ›  技术社区  ›  userxchange123

如何处理razor.cs文件中的异常并在控制台中全局显示它们?BlazorWebAssembly

  •  0
  • userxchange123  · 技术社区  · 3 年前

    我想创建一个中间件,如果程序中出现错误,它会捕捉到错误,并在浏览器控制台中显示其信息。

    我的意思是,如果razor.cs文件中出现错误,该功能将在浏览器控制台中写入有关该错误的信息,如错误的位置和类型。我的问题是,在blazor web组装应用程序中,哪个项目适合这样的中间件?可以是program.cs吗?或者,是否可以创建这样的中间件,使其位于一个位置,但影响所有razor.cs文件?有什么建议吗?

    0 回复  |  直到 3 年前
        1
  •  0
  •   mRizvandi    3 年前

    使用program.cs并在asp.net管道上添加中间件。

    这是一个AdvanceExceptionHandler示例:

    namespace AryaVtd.Orca.Server.Infrastructure.Middlewares.Logging
    {
        public class AdvancedExceptionHandler
        {
            private readonly RequestDelegate _next;
            private readonly ILogger _logger;
            private readonly IWebHostEnvironment _env;
    
            public AdvancedExceptionHandler(RequestDelegate next, ILoggerFactory logger, IWebHostEnvironment env)
            {
                _next = next;
                _logger = logger.CreateLogger(typeof(AdvancedExceptionHandler).Name);
                _env = env;
            }
    
            public async Task Invoke(HttpContext context)
            {
                string message = null;
                HttpStatusCode httpStatusCode = HttpStatusCode.InternalServerError;
    
                try
                {
                    await _next(context);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"AryaVtd.Orca.Server.Infrastructure.Middlewares.Logging:\r\nSource: {ex.Source} \r\nMessage: {ex.GetMessages()}");
    
                    if (_env.IsDevelopment())
                    {
                        var dic = new Dictionary<string, string>
                        {
                            ["StackTrace"] = ex.StackTrace,
                            ["Exception"] = ex.Message
                        };
                        message = JsonConvert.SerializeObject(dic);
                    }
                    else
                    {
                        message = "AdvancedExceptionHandler: unfortunately an error has occurred on server!";
                    }
    
                    await WriteToReponseAsync();
                }
    
                async Task WriteToReponseAsync()
                {
                    var exceptionResult = new ExceptionResult(message, (int)httpStatusCode);
                    var result = JsonConvert.SerializeObject(exceptionResult);
                    context.Response.StatusCode = (int)httpStatusCode;
                    context.Response.ContentType = "application/json";
    
                    await context.Response.WriteAsync(result);
                }
            }
        }
    
        public static class ExceptionHandlerMiddlewareExtension
        {
            public static void UseAdvancedExceptionHandler(this IApplicationBuilder app)
            {
                app.UseMiddleware<AdvancedExceptionHandler>();
            }
        }
    }
    

    呼叫应用程序。在program.cs的早期使用AdvancedExceptionHandler()。

    app.UseAdvancedExceptionHandler();