using System; using System.Diagnostics; using System.Linq; using System.Text; using Hncore.Infrastructure.Common; using Hncore.Infrastructure.Extension; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Hncore.Infrastructure.Core.Web; namespace Hncore.Infrastructure.EF { public class TraceLogger : ILogger { private readonly string categoryName; private IHttpContextAccessor _httpContextAccessor; public TraceLogger(string categoryName, IHttpContextAccessor httpContextAccessor) { this.categoryName = categoryName; this._httpContextAccessor = httpContextAccessor; } public bool IsEnabled(LogLevel logLevel) => true; public void Log( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { if (logLevel == LogLevel.Information && categoryName == "Microsoft.EntityFrameworkCore.Database.Command") { Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 执行sql:"); if (exception != null) { Console.WriteLine("发生异常:\n" + exception); } else { if (state.GetType().Name == "LogValues`6") { var paramText = state.GetType().GetField("_value1").GetValue(state).ToString(); var sql = state.GetType().GetField("_value5").GetValue(state).ToString(); var paramList = paramText.RegexMatches("@__.*?='.*?'"); paramList.ForEach(param => { var arr = param.Split('='); sql = sql.Replace(arr[0], arr[1]); }); Console.WriteLine(sql); if (_httpContextAccessor?.HttpContext?.Request?.Headers != null && _httpContextAccessor.HttpContext.Request.Headers .ContainsKey("enable-ef-log").ToBool()) { StringBuilder log = new StringBuilder(); log.Append("请求URL:" + _httpContextAccessor.HttpContext.Request.GetAbsoluteUri() + ""); log.Append("\nMethod:" + _httpContextAccessor.HttpContext.Request.Method + "\n"); if (_httpContextAccessor.HttpContext.Request.Method.ToLower() != "get") { log.Append("Body:\n" + _httpContextAccessor.HttpContext.Items["___requestbody"] + "\n------------------------\n"); } else { log.Append("\n------------------------\n"); } log.Append(sql); LogHelper.Debug("efcore日志", log.ToString()); } } } //Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} {logLevel} {eventId.Id} {this.categoryName}"); //Console.WriteLine(formatter(state, exception)); } } public IDisposable BeginScope(TState state) => null; } public class TraceLoggerProvider : ILoggerProvider { private IHttpContextAccessor _httpContextAccessor; public TraceLoggerProvider(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public ILogger CreateLogger(string categoryName) => new TraceLogger(categoryName, _httpContextAccessor); public void Dispose() { } } public static class DebugLog { public static void EnableDebugTrace(this DbContextOptionsBuilder optionsBuilder, IHttpContextAccessor httpContextAccessor) { LoggerFactory loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new TraceLoggerProvider(httpContextAccessor)); optionsBuilder.UseLoggerFactory(loggerFactory); optionsBuilder.EnableSensitiveDataLogging(); } } }