diff --git a/langguanApi.xml b/langguanApi.xml index 1a5125a..4431f0c 100644 --- a/langguanApi.xml +++ b/langguanApi.xml @@ -583,11 +583,11 @@ 日志 - + 构参 - + @@ -1231,6 +1231,21 @@ 国几排放量 + + + 日志实体类 + + + + + 日志等级 + + + + + 日志内容 + + 菜单实体类 @@ -2423,6 +2438,24 @@ + + + 日志服务 + + + + + 日志服务 + + + + + + 添加日志 + + + + 新增菜单 diff --git a/langguanApi/Middleware/CustomerExceptionFilter.cs b/langguanApi/Middleware/CustomerExceptionFilter.cs index 4da94ac..944cc49 100644 --- a/langguanApi/Middleware/CustomerExceptionFilter.cs +++ b/langguanApi/Middleware/CustomerExceptionFilter.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using langguanApi.Service; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; namespace langguanApi.Middleware @@ -8,21 +9,21 @@ namespace langguanApi.Middleware /// /// 日志 /// - public ILogger _logger; + public LogService _logger; /// /// 构参 /// - /// - public CustomerExceptionFilter(ILoggerFactory loggerFactory) + /// + public CustomerExceptionFilter(LogService logService) { - _logger = loggerFactory.CreateLogger(); + _logger = logService; } /// /// 重写 /// /// /// - public Task OnExceptionAsync(ExceptionContext context) + public async Task OnExceptionAsync(ExceptionContext context) { if (context.ExceptionHandled == false) { @@ -30,11 +31,18 @@ namespace langguanApi.Middleware context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(json); } - _logger.LogError($"请求出现异常,地址:{context.HttpContext?.Request?.Path}," + + await _logger.addLog(new Model.Entity.LogEntity() + { + Path = context.HttpContext.Request.Path, + msg = context.Exception.Message, + Level = "Error" + }); + Console.WriteLine($"请求出现异常,地址:{context.HttpContext?.Request?.Path}," + $"请求方式:{context.HttpContext.Request.Method},异常信息:{context.Exception.Message}"); + //记录异常已经处理 context.ExceptionHandled = true; - return Task.CompletedTask; + // return Task.CompletedTask; } } } diff --git a/langguanApi/Model/Entity/LogEntity.cs b/langguanApi/Model/Entity/LogEntity.cs new file mode 100644 index 0000000..2e8db97 --- /dev/null +++ b/langguanApi/Model/Entity/LogEntity.cs @@ -0,0 +1,19 @@ +namespace langguanApi.Model.Entity +{ + /// + /// 日志实体类 + /// + public class LogEntity : BaseModel + { + public string Path { get; set; } + /// + /// 日志等级 + /// + public string Level { get; set; } + /// + /// 日志内容 + /// + public string msg { get; set; } + + } +} diff --git a/langguanApi/Service/LogService.cs b/langguanApi/Service/LogService.cs new file mode 100644 index 0000000..7238790 --- /dev/null +++ b/langguanApi/Service/LogService.cs @@ -0,0 +1,33 @@ +using langguanApi.Common.Proxy; +using langguanApi.Extensions.AutoDI; +using langguanApi.Model; +using langguanApi.Model.Entity; + +namespace langguanApi.Service +{ + /// + /// 日志服务 + /// + [ServiceInjection(InjectionType.Transient)] + public class LogService : BaseService + { + private readonly IConfiguration _configuration; + /// + /// 日志服务 + /// + /// + public LogService(IConfiguration config) : base(config, nameof(LogEntity)) + { + _configuration = config; + } + /// + /// 添加日志 + /// + /// + /// + public async Task addLog(LogEntity log) + { + await base.CreateAsync(log); + } + } +}