亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Asp.Net Core Razor Pages 異常處理中斷應用程序

Asp.Net Core Razor Pages 異常處理中斷應用程序

C#
慕田峪4524236 2023-04-29 15:47:53
我正在構建一個 Asp.Net Core 2.2 Razor Pages 應用程序。我正在編寫一個全局異常處理以避免在許多地方出現 try catch 塊。我關注了文章 -異常處理參考這是演示解決方案鏈接這是我的代碼,在Startup.cs中if(env.IsDevelopment()){    //app.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions {    //    SourceCodeLineCount = 2    //});    //app.UseDatabaseErrorPage();    app.UseExceptionHandler("/Error");    app.UseStatusCodePagesWithReExecute("/Error/{0}");    app.UseExceptionMiddleware();} else {...}在ExceptionMiddleware.cs中public ExceptionMiddleware(RequestDelegate next,IMailService emailSender){    _next = next;    _emailSender = emailSender;}public async Task InvokeAsync(HttpContext context){    try    {        await _next(context);    } catch(Exception ex)    {        EmailException(context,ex);    }}private async void EmailException(HttpContext context,Exception ex){    var uaString = context.Request.Headers["User-Agent"].ToString();    var ipAnonymizedString = context.Connection.RemoteIpAddress.AnonymizeIP();    var userId = "Unknown";    var profileId = "Unknown";    if(context.User.Identity.IsAuthenticated)    {        userId = context.User.FindFirstValue(ClaimTypes.NameIdentifier);        profileId = context.User.GetUserClaim(ClaimsKey.ProfileId);    }    var sb = new StringBuilder($"An error has occurred on {context.Request.Host}. \r\n \r\n");    sb.Append($"Path = {context.Request.Path} \r\n \r\n");    sb.Append($"Error Message = {ex.Message} \r\n");    sb.Append($"Error Source = {ex.Source} - {profileId} \r\n");    if(ex.InnerException != null)    {        sb.Append($"Inner Exception = {ex.InnerException.ToString()} \r\n");    } else    {        sb.Append("Inner Exception = null \r\n");    }    sb.Append($"Error StackTrace = {ex.StackTrace} \r\n");    await _emailSender.SendMasterEmailAsync($"Error on {context.Request.Host}.",sb.ToString(),uaString,ipAnonymizedString,userId);    throw ex;}
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號