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.

CustomizeStopWatch.cs 805B

1234567891011121314151617181920212223242526272829303132
  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; }
  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}");
  23. _sw.Stop();
  24. }
  25. }
  26. }
  27. }