日志记录

This commit is contained in:
yanghongwei 2024-07-16 21:41:16 +08:00
parent 9a2265d4ae
commit 232dbb988e
4 changed files with 103 additions and 10 deletions

View File

@ -583,11 +583,11 @@
日志 日志
</summary> </summary>
</member> </member>
<member name="M:langguanApi.Middleware.CustomerExceptionFilter.#ctor(Microsoft.Extensions.Logging.ILoggerFactory)"> <member name="M:langguanApi.Middleware.CustomerExceptionFilter.#ctor(langguanApi.Service.LogService)">
<summary> <summary>
构参 构参
</summary> </summary>
<param name="loggerFactory"></param> <param name="logService"></param>
</member> </member>
<member name="M:langguanApi.Middleware.CustomerExceptionFilter.OnExceptionAsync(Microsoft.AspNetCore.Mvc.Filters.ExceptionContext)"> <member name="M:langguanApi.Middleware.CustomerExceptionFilter.OnExceptionAsync(Microsoft.AspNetCore.Mvc.Filters.ExceptionContext)">
<summary> <summary>
@ -1231,6 +1231,21 @@
国几排放量 国几排放量
</summary> </summary>
</member> </member>
<member name="T:langguanApi.Model.Entity.LogEntity">
<summary>
日志实体类
</summary>
</member>
<member name="P:langguanApi.Model.Entity.LogEntity.Level">
<summary>
日志等级
</summary>
</member>
<member name="P:langguanApi.Model.Entity.LogEntity.msg">
<summary>
日志内容
</summary>
</member>
<member name="T:langguanApi.Model.Entity.Menu"> <member name="T:langguanApi.Model.Entity.Menu">
<summary> <summary>
菜单实体类 菜单实体类
@ -2423,6 +2438,24 @@
<param name="input"></param> <param name="input"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:langguanApi.Service.LogService">
<summary>
日志服务
</summary>
</member>
<member name="M:langguanApi.Service.LogService.#ctor(Microsoft.Extensions.Configuration.IConfiguration)">
<summary>
日志服务
</summary>
<param name="config"></param>
</member>
<member name="M:langguanApi.Service.LogService.addLog(langguanApi.Model.Entity.LogEntity)">
<summary>
添加日志
</summary>
<param name="log"></param>
<returns></returns>
</member>
<member name="M:langguanApi.Service.MenuService.AddMenu(langguanApi.Model.Entity.AddMenuDto)"> <member name="M:langguanApi.Service.MenuService.AddMenu(langguanApi.Model.Entity.AddMenuDto)">
<summary> <summary>
新增菜单 新增菜单

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using langguanApi.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
namespace langguanApi.Middleware namespace langguanApi.Middleware
@ -8,21 +9,21 @@ namespace langguanApi.Middleware
/// <summary> /// <summary>
/// 日志 /// 日志
/// </summary> /// </summary>
public ILogger _logger; public LogService _logger;
/// <summary> /// <summary>
/// 构参 /// 构参
/// </summary> /// </summary>
/// <param name="loggerFactory"></param> /// <param name="logService"></param>
public CustomerExceptionFilter(ILoggerFactory loggerFactory) public CustomerExceptionFilter(LogService logService)
{ {
_logger = loggerFactory.CreateLogger<CustomerExceptionFilter>(); _logger = logService;
} }
/// <summary> /// <summary>
/// 重写 /// 重写
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <returns></returns> /// <returns></returns>
public Task OnExceptionAsync(ExceptionContext context) public async Task OnExceptionAsync(ExceptionContext context)
{ {
if (context.ExceptionHandled == false) if (context.ExceptionHandled == false)
{ {
@ -30,11 +31,18 @@ namespace langguanApi.Middleware
context.HttpContext.Response.StatusCode = 400; context.HttpContext.Response.StatusCode = 400;
context.Result = new JsonResult(json); 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.HttpContext.Request.Method},异常信息:{context.Exception.Message}");
//记录异常已经处理 //记录异常已经处理
context.ExceptionHandled = true; context.ExceptionHandled = true;
return Task.CompletedTask; // return Task.CompletedTask;
} }
} }
} }

View File

@ -0,0 +1,19 @@
namespace langguanApi.Model.Entity
{
/// <summary>
/// 日志实体类
/// </summary>
public class LogEntity : BaseModel
{
public string Path { get; set; }
/// <summary>
/// 日志等级
/// </summary>
public string Level { get; set; }
/// <summary>
/// 日志内容
/// </summary>
public string msg { get; set; }
}
}

View File

@ -0,0 +1,33 @@
using langguanApi.Common.Proxy;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using langguanApi.Model.Entity;
namespace langguanApi.Service
{
/// <summary>
/// 日志服务
/// </summary>
[ServiceInjection(InjectionType.Transient)]
public class LogService : BaseService<LogEntity>
{
private readonly IConfiguration _configuration;
/// <summary>
/// 日志服务
/// </summary>
/// <param name="config"></param>
public LogService(IConfiguration config) : base(config, nameof(LogEntity))
{
_configuration = config;
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
public async Task addLog(LogEntity log)
{
await base.CreateAsync(log);
}
}
}