You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DS.Module.Core.Log;
using DS.Module.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DS.WMS.JobService
{
/// <summary>
/// 全局异常错误日志
/// </summary>
public class GlobalExceptionsFilter : IExceptionFilter
{
private readonly IHostingEnvironment _env;
private readonly IServiceProvider _serviceProvider;
public GlobalExceptionsFilter(IHostingEnvironment env, IServiceProvider serviceProvider)
{
_env = env;
_serviceProvider = serviceProvider;
}
public void OnException(ExceptionContext context)
{
var json = new JsonErrorResponse();
json.Message = context.Exception.Message; //错误信息
if (_env.IsDevelopment())
{
json.DevelopmentMessage = context.Exception.StackTrace; //堆栈信息
}
var result = DataResult<Object>.Failed(json.Message);
var objectResult = new BadRequestObjectResult(result);
objectResult.StatusCode = (int?)StatusCodes.Status200OK;
context.Result = objectResult;
// context.Result = new InternalServerErrorObjectResult(json);
//MiniProfiler.Current.CustomTiming("Errors", json.Message);
//记录异常到日志
StringBuilder exMsg = new();
exMsg.AppendLine($"【异常方法:】{context.HttpContext.Request.Path}");
exMsg.AppendLine($"【请求类型:】{context.HttpContext.Request.Method}");
exMsg.AppendLine($"【异常错误:】{context.Exception.Message}");
exMsg.AppendLine($"【堆栈跟踪:】{context.Exception.StackTrace}");
#region 写入日志库
var className = context.Exception.TargetSite.DeclaringType?.FullName;
var groupCollection = Regex.Match(className, "<(.*?)>").Groups;
var methodName = "";
if (groupCollection.Count > 1)
{
methodName = groupCollection[1].Value;
}
var exLog = new SysLogException()
{
ClassName = className,
MethodName = methodName,
ExceptionName = context.Exception.Message,
ExceptionMsg = context.Exception.Message,
ExceptionSource = context.Exception.Source,
StackTrace = context.Exception.StackTrace,
ParamsObj = context.Exception.TargetSite.GetParameters().ToString(),
};
#endregion
// Logger.Log(NLog.LogLevel.Info, "异常信息:" + JsonConvert.SerializeObject(context.Exception));
//采用log4net 进行错误日志记录
//_loggerHelper.Error(json.Message, WriteLog(json.Message, context.Exception));
//_hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait();
}
/// <summary>
/// 自定义返回格式
/// </summary>
/// <param name="throwMsg"></param>
/// <param name="ex"></param>
/// <returns></returns>
public string WriteLog(string throwMsg, Exception ex)
{
return string.Format("【自定义错误】:{0} \r\n【异常类型】{1} \r\n【异常信息】{2} \r\n【堆栈调用】{3}", new object[]
{
throwMsg,
ex.GetType().Name, ex.Message, ex.StackTrace
});
}
}
/// <summary>
///
/// </summary>
public class InternalServerErrorObjectResult : ObjectResult
{
public InternalServerErrorObjectResult(object value) : base(value)
{
StatusCode = StatusCodes.Status500InternalServerError;
}
}
/// <summary>
/// 返回错误信息
/// </summary>
public class JsonErrorResponse
{
/// <summary>
/// 生产环境的消息
/// </summary>
public string Message { get; set; }
/// <summary>
/// 开发环境的消息
/// </summary>
public string DevelopmentMessage { get; set; }
}
}