using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HealthMonitor.Common { public class CustomizeStopWatch : IDisposable { private readonly Stopwatch _sw; private readonly string _domain; private readonly ILogger _logger; public string Content { get; set; } = default!; public CustomizeStopWatch(string domain, ILogger logger) { _domain = domain; _logger = logger; _sw = new Stopwatch(); _sw.Start(); } public void Dispose() { if (_sw != null) { _logger.LogInformation($"统计时间[{_domain}],耗时 {_sw.Elapsed.TotalMilliseconds} 毫秒 {Content}"); _sw.Stop(); } } } }