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.

84 lines
1.9KB

  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) where TState : notnull
  17. {
  18. return new NoopDisposable();
  19. }
  20. //public IDisposable BeginScope<TState>(TState state)
  21. //{
  22. // //ScopeStack.Push(state.ToString());
  23. // return new NoopDisposable();
  24. //}
  25. public bool IsEnabled(LogLevel logLevel)
  26. {
  27. return logLevel >= EfCoreLogProvider.LogThisAndAbout;
  28. }
  29. public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception, string> formatter)
  30. {
  31. switch (logLevel)
  32. {
  33. case LogLevel.Trace:
  34. case LogLevel.Debug:
  35. {
  36. var content = formatter(state, exception!).Replace(Path.DirectorySeparatorChar, '\t');
  37. Serilog.Log.Debug(content);
  38. }
  39. break;
  40. case LogLevel.Information:
  41. {
  42. var content = formatter(state, exception!).Replace(Path.DirectorySeparatorChar, '\t');
  43. Serilog.Log.Information(content);
  44. }
  45. break;
  46. case LogLevel.Warning:
  47. {
  48. var content = formatter(state, exception!).Replace(Path.DirectorySeparatorChar, '\t');
  49. Serilog.Log.Warning(content);
  50. }
  51. break;
  52. case LogLevel.Error:
  53. Serilog.Log.Error(formatter(state, exception!));
  54. break;
  55. case LogLevel.Critical:
  56. Serilog.Log.Fatal(formatter(state, exception!));
  57. break;
  58. }
  59. }
  60. private class NoopDisposable : IDisposable
  61. {
  62. public void Dispose()
  63. {
  64. //while (!ScopeStack.TryPop(out _))
  65. //{
  66. // Thread.Sleep(100);
  67. //}
  68. }
  69. }
  70. }
  71. }