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.
jellyfin/MediaBrowser.Controller/Library/Profiler.cs

84 lines
2.5 KiB

using System;
6 years ago
using System.Diagnostics;
4 years ago
using System.Globalization;
using Microsoft.Extensions.Logging;
6 years ago
namespace MediaBrowser.Controller.Library
{
/// <summary>
/// Class Profiler.
6 years ago
/// </summary>
public class Profiler : IDisposable
{
/// <summary>
/// The name.
6 years ago
/// </summary>
readonly string _name;
4 years ago
6 years ago
/// <summary>
/// The stopwatch.
6 years ago
/// </summary>
readonly Stopwatch _stopwatch;
/// <summary>
/// The _logger.
6 years ago
/// </summary>
private readonly ILogger<Profiler> _logger;
6 years ago
/// <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)
6 years ago
{
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);
4 years ago
GC.SuppressFinalize(this);
6 years ago
}
/// <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)
{
4 years ago
message = string.Format(
CultureInfo.InvariantCulture,
"{0} took {1} minutes.",
_name,
((float)_stopwatch.ElapsedMilliseconds / 60000).ToString("F", CultureInfo.InvariantCulture));
6 years ago
}
else
{
4 years ago
message = string.Format(
CultureInfo.InvariantCulture,
"{0} took {1} seconds.",
_name,
((float)_stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000", CultureInfo.InvariantCulture));
6 years ago
}
_logger.LogInformation(message);
6 years ago
}
}
}
}