Net.Like.Xue.Tokyo/Assets/BITKit/Core/Utility/StopWatcher.cs

28 lines
760 B
C#

using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace BITKit
{
public class StopWatcher:IDisposable
{
private readonly Stopwatch _stopwatch = new Stopwatch();
private readonly ILogger _logger;
private readonly string _message;
public StopWatcher(ILogger logger,string message=null)
{
_logger = logger;
_message = message;
_logger.LogInformation($"开始计时:{message}");
_stopwatch.Start();
}
public void Dispose()
{
_stopwatch.Stop();
_logger.LogInformation($"已完成:{_message},耗时{_stopwatch.ElapsedMilliseconds}ms");
}
}
}