From ec92b3fa3ada2026fa503ec55319df44879fa27c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Fri, 12 May 2023 08:02:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/SwaggerExtension.cs | 2 +- .../Middleware/GlobalExceptionMiddleware.cs | 14 ++++++++++- ZR.Common/WxNoticeHelper.cs | 23 +++++++++++-------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs index b51c127..2038144 100644 --- a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs +++ b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs @@ -44,7 +44,7 @@ namespace ZR.Admin.WebApi.Extensions { c.SwaggerDoc("v1", new OpenApiInfo { - Title = "ZrAdmin.NET Api - .NET6", + Title = "ZrAdmin.NET Api", Version = "v1", Description = "", }); diff --git a/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs b/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs index 6dd96ed..f6efad4 100644 --- a/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs +++ b/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs @@ -4,6 +4,7 @@ using Infrastructure.Model; using IPTools.Core; using Microsoft.AspNetCore.Http.Features; using NLog; +using System.Diagnostics; using System.Text.Encodings.Web; using System.Text.Json; using ZR.Admin.WebApi.Extensions; @@ -117,7 +118,18 @@ namespace ZR.Admin.WebApi.Middleware Logger.Log(ei); context.Response.ContentType = "text/json;charset=utf-8"; await context.Response.WriteAsync(responseResult, System.Text.Encoding.UTF8); - WxNoticeHelper.SendMsg("系统出错", sysOperLog.ErrorMsg); + + // 获取异常堆栈 + var traceFrame = new StackTrace(true)?.GetFrame(0); + // 获取出错的文件名 + var exceptionFileName = traceFrame?.GetFileName(); + // 获取出错的行号 + var exceptionFileLineNumber = traceFrame?.GetFileLineNumber(); + string errorMsg = $"用户名:{sysOperLog.OperName}\n" + + $"错误信息:{sysOperLog.ErrorMsg}\n"+ + $"错误行号:{exceptionFileLineNumber}\n" + + $"{traceFrame}#{exceptionFileName}"; + WxNoticeHelper.SendMsg("系统出错", errorMsg); SysOperLogService.InsertOperlog(sysOperLog); } diff --git a/ZR.Common/WxNoticeHelper.cs b/ZR.Common/WxNoticeHelper.cs index 0f7fa3a..976feb6 100644 --- a/ZR.Common/WxNoticeHelper.cs +++ b/ZR.Common/WxNoticeHelper.cs @@ -41,6 +41,7 @@ namespace ZR.Common } if (string.IsNullOrEmpty(CORPID)) { + System.Console.WriteLine("请完成企业微信配置"); return (0, "请完成企业微信通知配置"); } GetTokenResult tokenResult = GetAccessToken(); @@ -74,7 +75,7 @@ namespace ZR.Common //{"errcode":0,"errmsg":"ok","invaliduser":""} string msgResult = HttpHelper.HttpPost(msgUrl, postData, "contentType/json"); GetTokenResult getTokenResult = JsonSerializer.Deserialize(msgResult); - + System.Console.WriteLine(msgResult); return (getTokenResult?.errcode == 0 ? 100 : 0, getTokenResult?.errmsg); } public static (int, string) SendMsg(string title, string content, string toUser) @@ -92,7 +93,7 @@ namespace ZR.Common { string getTokenUrl = $"{GetTokenUrl}?corpid={CORPID}&corpsecret={CORPSECRET}"; string getTokenResult = HttpHelper.HttpGet(getTokenUrl); - + System.Console.WriteLine(getTokenResult); GetTokenResult tokenResult = JsonSerializer.Deserialize(getTokenResult); return tokenResult; } @@ -131,16 +132,18 @@ namespace ZR.Common { Dictionary dic = new() { - { "touser", toUser }, - { "msgtype", "markdown" }, - { "agentid", AGENTID }, - { "enable_duplicate_check",1} - }; - - dic.Add("markdown", new Dictionary + { "touser", toUser }, + { "msgtype", "markdown" }, + { "agentid", AGENTID }, + { "enable_duplicate_check", 1 }, + { + "markdown", + new Dictionary { { "content", $"**{title}**\n\n{content}" } - }); + } + } + }; return dic; }