using Microsoft.Extensions.Logging; using System.Diagnostics; namespace HealthMonitor.Core.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}", _domain, _sw.Elapsed.TotalMilliseconds, Content); _sw.Stop(); } GC.SuppressFinalize(this); } } }