mh_frame_sps/APT.Infrastructure.EF/EFDbContextLog.cs

37 lines
1.2 KiB
C#
Raw Normal View History

2026-04-07 13:47:52 +08:00
using APT.Infrastructure.Core;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
namespace APT.Infrastructure.EF
{
public class EFDbContextLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName) => new EFDbContextLogger(categoryName);
public void Dispose() { }
}
public class EFDbContextLogger : ILogger
{
private readonly string categoryName;
public EFDbContextLogger(string categoryName) => this.categoryName = categoryName;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
//ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command")
{
var logContent = formatter(state, exception);
LoggerManager.GetLogger("SQLLog").
Info(logContent + System.Environment.NewLine +
(exception != null ? ("Except:" + exception.Message) : string.Empty));
}
}
public IDisposable BeginScope<TState>(TState state) => null;
}
}