2
0

Profiler.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Diagnostics;
  4. namespace MediaBrowser.Controller.Library
  5. {
  6. /// <summary>
  7. /// Class Profiler
  8. /// </summary>
  9. public class Profiler : IDisposable
  10. {
  11. /// <summary>
  12. /// The name
  13. /// </summary>
  14. readonly string name;
  15. /// <summary>
  16. /// The stopwatch
  17. /// </summary>
  18. readonly Stopwatch stopwatch;
  19. /// <summary>
  20. /// The _logger
  21. /// </summary>
  22. private ILogger _logger;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="Profiler" /> class.
  25. /// </summary>
  26. /// <param name="name">The name.</param>
  27. /// <param name="logger">The logger.</param>
  28. public Profiler(string name, ILogger logger)
  29. {
  30. this.name = name;
  31. _logger = logger;
  32. stopwatch = new Stopwatch();
  33. stopwatch.Start();
  34. }
  35. #region IDisposable Members
  36. /// <summary>
  37. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  38. /// </summary>
  39. public void Dispose()
  40. {
  41. Dispose(true);
  42. GC.SuppressFinalize(this);
  43. }
  44. /// <summary>
  45. /// Releases unmanaged and - optionally - managed resources.
  46. /// </summary>
  47. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  48. protected virtual void Dispose(bool dispose)
  49. {
  50. if (dispose)
  51. {
  52. stopwatch.Stop();
  53. string message;
  54. if (stopwatch.ElapsedMilliseconds > 300000)
  55. {
  56. message = string.Format("{0} took {1} minutes.",
  57. name, ((float)stopwatch.ElapsedMilliseconds / 60000).ToString("F"));
  58. }
  59. else
  60. {
  61. message = string.Format("{0} took {1} seconds.",
  62. name, ((float)stopwatch.ElapsedMilliseconds / 1000).ToString("#0.000"));
  63. }
  64. _logger.Info(message);
  65. }
  66. }
  67. #endregion
  68. }
  69. }