Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EfCoreLogger.cs 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.IO;
  5. using System.Threading;
  6. namespace HealthMonitor.WebApi.DbLog
  7. {
  8. public class EfCoreLogger : ILogger
  9. {
  10. private readonly string _categoryName;
  11. //protected static ConcurrentStack<string> ScopeStack { get; } = new ConcurrentStack<string>();
  12. public EfCoreLogger(string categoryName)
  13. {
  14. _categoryName = categoryName;
  15. }
  16. public IDisposable BeginScope<TState>(TState state)
  17. {
  18. //ScopeStack.Push(state.ToString());
  19. return new NoopDisposable();
  20. }
  21. public bool IsEnabled(LogLevel logLevel)
  22. {
  23. return logLevel >= EfCoreLogProvider.LogThisAndAbout;
  24. }
  25. public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
  26. {
  27. switch (logLevel)
  28. {
  29. case LogLevel.Trace:
  30. case LogLevel.Debug:
  31. {
  32. var content = formatter(state, exception).Replace(Path.DirectorySeparatorChar, '\t');
  33. Serilog.Log.Debug(content);
  34. }
  35. break;
  36. case LogLevel.Information:
  37. {
  38. var content = formatter(state, exception).Replace(Path.DirectorySeparatorChar, '\t');
  39. Serilog.Log.Information(content);
  40. }
  41. break;
  42. case LogLevel.Warning:
  43. {
  44. var content = formatter(state, exception).Replace(Path.DirectorySeparatorChar, '\t');
  45. Serilog.Log.Warning(content);
  46. }
  47. break;
  48. case LogLevel.Error:
  49. Serilog.Log.Error(formatter(state, exception));
  50. break;
  51. case LogLevel.Critical:
  52. Serilog.Log.Fatal(formatter(state, exception));
  53. break;
  54. }
  55. }
  56. private class NoopDisposable : IDisposable
  57. {
  58. public void Dispose()
  59. {
  60. //while (!ScopeStack.TryPop(out _))
  61. //{
  62. // Thread.Sleep(100);
  63. //}
  64. }
  65. }
  66. }
  67. }