|
@@ -1,6 +1,9 @@
|
|
|
|
|
|
using System;
|
|
|
+using Hotline.Logger.Models;
|
|
|
+using Hotline.Share.Tools;
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Serilog;
|
|
|
|
|
@@ -16,17 +19,20 @@ namespace HotPot.Mvc.Filters
|
|
|
/// </summary>
|
|
|
private readonly Serilog.ILogger _logger;
|
|
|
|
|
|
- public ErrorHandlingFilter(ILogger logger)
|
|
|
+ private readonly IConfiguration _configuration;
|
|
|
+
|
|
|
+ public ErrorHandlingFilter(ILogger logger, IConfiguration configuration)
|
|
|
{
|
|
|
+ _configuration = configuration;
|
|
|
_logger = new LoggerConfiguration()
|
|
|
- .WriteTo.Logger(configure => configure
|
|
|
- .MinimumLevel.Debug()
|
|
|
- .WriteTo.RollingFile(
|
|
|
- Path.Combine("logs", "acc-log-{Date}.txt"),
|
|
|
- retainedFileCountLimit: 7,
|
|
|
- outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
|
|
|
- ))
|
|
|
- .CreateLogger();
|
|
|
+ .WriteTo.Logger(configure => configure
|
|
|
+ .MinimumLevel.Debug()
|
|
|
+ .WriteTo.RollingFile(
|
|
|
+ Path.Combine("logs", "err-log-{Date}.txt"),
|
|
|
+ retainedFileCountLimit: 7,
|
|
|
+ outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
|
|
|
+ ))
|
|
|
+ .CreateLogger();
|
|
|
|
|
|
}
|
|
|
|
|
@@ -36,24 +42,19 @@ namespace HotPot.Mvc.Filters
|
|
|
/// <param name="context">异常</param>
|
|
|
public void OnException(ExceptionContext context)
|
|
|
{
|
|
|
- // MvcLog.Exception(context.Exception);
|
|
|
- var logger = ServiceLocator.Instance.GetService<ILogOut>();
|
|
|
- try
|
|
|
- {
|
|
|
- if (context.HttpContext.Request.Method.ToUpper() == "GET")
|
|
|
- {
|
|
|
- logger.Error(
|
|
|
- "Method:" + context.HttpContext.Request.Method + " Path:" + context.HttpContext.Request.Path,
|
|
|
- context.HttpContext.Request.QueryString,
|
|
|
- context.HttpContext.Request.Headers["token"],
|
|
|
- context
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Logs.Err("新的日志记录报错:" + e.Message);
|
|
|
- }
|
|
|
+ if (_configuration.GetSection("AccLog").Get<bool>() == false) return;
|
|
|
+ var exceptionModel = new ExceptionModel();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ context.HttpContext.Items.TryAdd("ErrorId", exceptionModel.Id);
|
|
|
+ exceptionModel.Context = context.Exception.ToJson();
|
|
|
+ exceptionModel.Message = context.Exception.Message;
|
|
|
+ _logger.Error(exceptionModel.ToJson());
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|