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.

79 lines
1.8KB

  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. }