EFLoggerProvider.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. namespace DBHelper
  4. {
  5. public class EFLoggerProvider : ILoggerProvider
  6. {
  7. public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName);
  8. public void Dispose()
  9. { }
  10. }
  11. public class EFLogger : ILogger
  12. {
  13. private readonly string categoryName;
  14. public EFLogger(string categoryName) => this.categoryName = categoryName;
  15. public bool IsEnabled(LogLevel logLevel) => true;
  16. public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
  17. {
  18. //ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
  19. if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
  20. && logLevel == LogLevel.Information)
  21. {
  22. var logContent = formatter(state, exception);
  23. if (logContent.Contains("SRM5") || logContent.Contains("STATION5") || logContent.Contains("RGV5") || logContent.Contains("BCR8")) return;
  24. DbLog.DBEX(logContent);
  25. //TODO: 拿到日志内容想怎么玩就怎么玩吧
  26. //Console.WriteLine();
  27. //Console.ForegroundColor = ConsoleColor.Green;
  28. //Console.WriteLine(logContent);
  29. //Console.ResetColor();
  30. }
  31. }
  32. public IDisposable BeginScope<TState>(TState state) => null;
  33. }
  34. }