1234567891011121314151617181920212223242526272829303132333435363738 |
- using Microsoft.Extensions.Logging;
- using System;
- namespace DBHelper
- {
- public class EfLoggerProvider : ILoggerProvider
- {
- public ILogger CreateLogger(string categoryName) => new EfLogger(categoryName);
- public void Dispose()
- { }
- }
- public class EfLogger : ILogger
- {
- private readonly string _categoryName;
- public EfLogger(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" ||
- logLevel != LogLevel.Information) return;
- var logContent = formatter(state, exception);
- //TODO: 拿到日志内容想怎么玩就怎么玩吧
- //Console.WriteLine();
- //Console.ForegroundColor = ConsoleColor.Green;
- //Console.WriteLine(logContent);
- //Console.ResetColor();
- }
- public IDisposable BeginScope<TState>(TState state) => null;
- }
- }
|