日志记录
This commit is contained in:
		
							parent
							
								
									9a2265d4ae
								
							
						
					
					
						commit
						232dbb988e
					
				| 
						 | 
				
			
			@ -583,11 +583,11 @@
 | 
			
		|||
            日志
 | 
			
		||||
            </summary>
 | 
			
		||||
        </member>
 | 
			
		||||
        <member name="M:langguanApi.Middleware.CustomerExceptionFilter.#ctor(Microsoft.Extensions.Logging.ILoggerFactory)">
 | 
			
		||||
        <member name="M:langguanApi.Middleware.CustomerExceptionFilter.#ctor(langguanApi.Service.LogService)">
 | 
			
		||||
            <summary>
 | 
			
		||||
            构参
 | 
			
		||||
            </summary>
 | 
			
		||||
            <param name="loggerFactory"></param>
 | 
			
		||||
            <param name="logService"></param>
 | 
			
		||||
        </member>
 | 
			
		||||
        <member name="M:langguanApi.Middleware.CustomerExceptionFilter.OnExceptionAsync(Microsoft.AspNetCore.Mvc.Filters.ExceptionContext)">
 | 
			
		||||
            <summary>
 | 
			
		||||
| 
						 | 
				
			
			@ -1231,6 +1231,21 @@
 | 
			
		|||
            国几排放量
 | 
			
		||||
            </summary>
 | 
			
		||||
        </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">
 | 
			
		||||
            <summary>
 | 
			
		||||
            菜单实体类
 | 
			
		||||
| 
						 | 
				
			
			@ -2423,6 +2438,24 @@
 | 
			
		|||
            <param name="input"></param>
 | 
			
		||||
            <returns></returns>
 | 
			
		||||
        </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)">
 | 
			
		||||
            <summary>
 | 
			
		||||
            新增菜单
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		|||
        /// <summary>
 | 
			
		||||
        /// 日志
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public ILogger _logger;
 | 
			
		||||
        public LogService _logger;
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 构参
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="loggerFactory"></param>
 | 
			
		||||
        public CustomerExceptionFilter(ILoggerFactory loggerFactory)
 | 
			
		||||
        /// <param name="logService"></param>
 | 
			
		||||
        public CustomerExceptionFilter(LogService logService)
 | 
			
		||||
        {
 | 
			
		||||
            _logger = loggerFactory.CreateLogger<CustomerExceptionFilter>();
 | 
			
		||||
            _logger = logService;
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 重写
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="context"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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; }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue