e3f55a0c54
Co-authored-by: Patrick Barron <18354464+barronpm@users.noreply.github.com>
86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
#nullable disable
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace MediaBrowser.Controller.Library
|
|
{
|
|
/// <summary>
|
|
/// Class Profiler.
|
|
/// </summary>
|
|
public class Profiler : IDisposable
|
|
{
|
|
/// <summary>
|
|
/// The name.
|
|
/// </summary>
|
|
private readonly string _name;
|
|
|
|
/// <summary>
|
|
/// The stopwatch.
|
|
/// </summary>
|
|
private readonly Stopwatch _stopwatch;
|
|
|
|
/// <summary>
|
|
/// The _logger.
|
|
/// </summary>
|
|
private readonly ILogger<Profiler> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Profiler" /> class.
|
|
/// </summary>
|
|
/// <param name="name">The name.</param>
|
|
/// <param name="logger">The logger.</param>
|
|
public Profiler(string name, ILogger<Profiler> logger)
|
|
{
|
|
this._name = name;
|
|
|
|
_logger = logger;
|
|
|
|
_stopwatch = new Stopwatch();
|
|
_stopwatch.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
/// </summary>
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
protected virtual void Dispose(bool dispose)
|
|
{
|
|
if (dispose)
|
|
{
|
|
_stopwatch.Stop();
|
|
string message;
|
|
if (_stopwatch.ElapsedMilliseconds > 300000)
|
|
{
|
|
message = string.Format(
|
|
CultureInfo.InvariantCulture,
|
|
"{0} took {1} minutes.",
|
|
_name,
|
|
((float)_stopwatch.ElapsedMilliseconds / 60000).ToString("F", CultureInfo.InvariantCulture));
|
|
}
|
|
else
|
|
{
|
|
message = string.Format(
|
|
CultureInfo.InvariantCulture,
|
|
"{0} took {1} seconds.",
|
|
_name,
|
|
((float)_stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000", CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
_logger.LogInformation(message);
|
|
}
|
|
}
|
|
}
|
|
}
|