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.

34 line
902B

  1. using Microsoft.Extensions.Logging;
  2. using System.Diagnostics;
  3. namespace HealthMonitor.Core.Common
  4. {
  5. public class CustomizeStopWatch : IDisposable
  6. {
  7. private readonly Stopwatch _sw;
  8. private readonly string _domain;
  9. private readonly ILogger _logger;
  10. public string Content { get; set; }=default!;
  11. public CustomizeStopWatch(string domain, ILogger logger)
  12. {
  13. _domain = domain;
  14. _logger = logger;
  15. _sw = new Stopwatch();
  16. _sw.Start();
  17. }
  18. public void Dispose()
  19. {
  20. if (_sw != null)
  21. {
  22. _logger.LogInformation("统计时间[{_domain}],耗时 {_sw.Elapsed.TotalMilliseconds} 毫秒 {Content}", _domain, _sw.Elapsed.TotalMilliseconds, Content);
  23. _sw.Stop();
  24. }
  25. GC.SuppressFinalize(this);
  26. }
  27. }
  28. }