You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.2 KiB

1 year ago
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Shentun.Utilities
  6. {
  7. public class EFLogger : ILogger
  8. {
  9. private readonly string categoryName;
  10. public EFLogger(string categoryName) => this.categoryName = categoryName;
  11. public bool IsEnabled(LogLevel logLevel) => true;
  12. public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
  13. {
  14. //ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
  15. if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
  16. && logLevel == LogLevel.Information)
  17. {
  18. var logContent = formatter(state, exception);
  19. //TODO: 拿到日志内容想怎么玩就怎么玩吧
  20. Console.WriteLine();
  21. Console.ForegroundColor = ConsoleColor.Green;
  22. Console.WriteLine(logContent);
  23. Console.ResetColor();
  24. }
  25. }
  26. public IDisposable BeginScope<TState>(TState state) => null;
  27. }
  28. }