using MediaBrowser.Model.Logging;
using System;
using System.Diagnostics;
namespace MediaBrowser.Controller.Library
{
///
/// Class Profiler
///
public class Profiler : IDisposable
{
///
/// The name
///
readonly string name;
///
/// The stopwatch
///
readonly Stopwatch stopwatch;
///
/// The _logger
///
private ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// The name.
/// The logger.
public Profiler(string name, ILogger logger)
{
this.name = name;
_logger = logger;
stopwatch = new Stopwatch();
stopwatch.Start();
}
#region IDisposable Members
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
stopwatch.Stop();
string message;
if (stopwatch.ElapsedMilliseconds > 300000)
{
message = string.Format("{0} took {1} minutes.",
name, ((float)stopwatch.ElapsedMilliseconds / 60000).ToString("F"));
}
else
{
message = string.Format("{0} took {1} seconds.",
name, ((float)stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000"));
}
_logger.Info(message);
}
}
#endregion
}
}