:zap:优化Nlog日志记录,解决记录到文本IP、请求地址获取不到问题

This commit is contained in:
不做码农
2023-09-07 09:34:01 +08:00
parent 02ffc18a61
commit b01765360d
6 changed files with 50 additions and 27 deletions
@@ -44,16 +44,18 @@ namespace ZR.ServiceCore.Middleware
private async Task HandleExceptionAsync(HttpContext context, Exception ex)
{
NLog.LogLevel logLevel = NLog.LogLevel.Info;
LogLevel logLevel = LogLevel.Info;
int code = (int)ResultCode.GLOBAL_ERROR;
string msg;
string error = string.Empty;
bool notice = true;
//自定义异常
if (ex is CustomException customException)
{
code = customException.Code;
msg = customException.Message;
error = customException.LogMsg;
notice = customException.Notice;
}
else if (ex is ArgumentException)//参数异常
{
@@ -64,7 +66,7 @@ namespace ZR.ServiceCore.Middleware
{
msg = "服务器好像出了点问题,请联系系统管理员...";
error = $"{ex.Message}";
logLevel = NLog.LogLevel.Error;
logLevel = LogLevel.Error;
context.Response.StatusCode = 500;
}
var options = new textJson.JsonSerializerOptions
@@ -117,15 +119,16 @@ namespace ZR.ServiceCore.Middleware
Logger.Log(ei);
context.Response.ContentType = "text/json;charset=utf-8";
await context.Response.WriteAsync(responseResult, System.Text.Encoding.UTF8);
string errorMsg = $"> 操作人:{sysOperLog.OperName}" +
$"\n> 操作地区:{sysOperLog.OperIp}({sysOperLog.OperLocation})" +
$"\n> 操作模块:{sysOperLog.Title}" +
$"\n> 操作地址:{sysOperLog.OperUrl}" +
$"\n> 错误信息:{msg}\n\n> {error}";
SysOperLogService.InsertOperlog(sysOperLog);
WxNoticeHelper.SendMsg("系统出错", errorMsg, "", WxNoticeHelper.MsgType.markdown);
SysOperLogService.InsertOperlog(sysOperLog);
if (!notice) return;
WxNoticeHelper.SendMsg("系统异常", errorMsg, msgType: WxNoticeHelper.MsgType.markdown);
}
public static Endpoint GetEndpoint(HttpContext context)
+2 -2
View File
@@ -52,13 +52,13 @@ namespace ZR.Service.System
{
logininfor.Msg = "用户名或密码错误";
AddLoginInfo(logininfor);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
}
if (user.Status == 1)
{
logininfor.Msg = "该用户已禁用";
AddLoginInfo(logininfor);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
}
logininfor.Status = "0";
+1 -1
View File
@@ -72,7 +72,7 @@ namespace ZR.ServiceCore.SqlSugar
{
if (showDbLog)
{
string log = $"【db{configId} SQL语句】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n";
string log = $"【db{configId} SQL】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n";
if (sql.TrimStart().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
{
logger.Info(log);