Logging.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #nullable disable
  2. using System.Diagnostics.Metrics;
  3. using System.Runtime.CompilerServices;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. namespace Terminal.Gui.App;
  7. /// <summary>
  8. /// Singleton logging instance class. Do not use console loggers
  9. /// with this class as it will interfere with Terminal.Gui
  10. /// screen output (i.e. use a file logger).
  11. /// </summary>
  12. /// <remarks>
  13. /// Also contains the
  14. /// <see cref="Meter"/> instance that should be used for internal metrics
  15. /// (iteration timing etc.).
  16. /// </remarks>
  17. public static class Logging
  18. {
  19. /// <summary>
  20. /// Logger, defaults to NullLogger (i.e. no logging). Set this to a
  21. /// file logger to enable logging of Terminal.Gui internals.
  22. /// </summary>
  23. public static ILogger Logger { get; set; } = NullLogger.Instance;
  24. /// <summary>
  25. /// Metrics reporting meter for internal Terminal.Gui processes. To use
  26. /// create your own static instrument e.g. CreateCounter, CreateHistogram etc
  27. /// </summary>
  28. internal static readonly Meter Meter = new ("Terminal.Gui");
  29. /// <summary>
  30. /// Metric for how long it takes each full iteration of the main loop to occur
  31. /// </summary>
  32. public static readonly Histogram<int> TotalIterationMetric = Meter.CreateHistogram<int> ("Iteration (ms)");
  33. /// <summary>
  34. /// Metric for how long it took to do the 'timeouts and invokes' section of main loop.
  35. /// </summary>
  36. public static readonly Histogram<int> IterationInvokesAndTimeouts = Meter.CreateHistogram<int> ("Invokes & Timers (ms)");
  37. /// <summary>
  38. /// Counter for when we redraw, helps detect situations e.g. where we are repainting entire UI every loop
  39. /// </summary>
  40. public static readonly Counter<int> Redraws = Meter.CreateCounter<int> ("Redraws");
  41. /// <summary>
  42. /// Metric for how long it takes to read all available input from the input stream - at which
  43. /// point input loop will sleep.
  44. /// </summary>
  45. public static readonly Histogram<int> DrainInputStream = Meter.CreateHistogram<int> ("Drain Input (ms)");
  46. /// <summary>
  47. /// Logs an error message including the class and method name.
  48. /// </summary>
  49. /// <param name="message"></param>
  50. /// <param name="caller"></param>
  51. /// <param name="filePath"></param>
  52. public static void Error (
  53. string message,
  54. [CallerMemberName] string caller = "",
  55. [CallerFilePath] string filePath = ""
  56. )
  57. {
  58. string className = Path.GetFileNameWithoutExtension (filePath);
  59. Logger.LogError ($"[{className}] [{caller}] {message}");
  60. }
  61. /// <summary>
  62. /// Logs a fatal/critical message including the class and method name.
  63. /// </summary>
  64. /// <param name="message"></param>
  65. /// <param name="caller"></param>
  66. /// <param name="filePath"></param>
  67. public static void Critical (
  68. string message,
  69. [CallerMemberName] string caller = "",
  70. [CallerFilePath] string filePath = ""
  71. )
  72. {
  73. string className = Path.GetFileNameWithoutExtension (filePath);
  74. Logger.LogCritical ($"[{className}] [{caller}] {message}");
  75. }
  76. /// <summary>
  77. /// Logs a debug message including the class and method name.
  78. /// </summary>
  79. /// <param name="message"></param>
  80. /// <param name="caller"></param>
  81. /// <param name="filePath"></param>
  82. public static void Debug (
  83. string message,
  84. [CallerMemberName] string caller = "",
  85. [CallerFilePath] string filePath = ""
  86. )
  87. {
  88. string className = Path.GetFileNameWithoutExtension (filePath);
  89. Logger.LogDebug ($"[{className}] [{caller}] {message}");
  90. }
  91. /// <summary>
  92. /// Logs an informational message including the class and method name.
  93. /// </summary>
  94. /// <param name="message"></param>
  95. /// <param name="caller"></param>
  96. /// <param name="filePath"></param>
  97. public static void Information (
  98. string message,
  99. [CallerMemberName] string caller = "",
  100. [CallerFilePath] string filePath = ""
  101. )
  102. {
  103. string className = Path.GetFileNameWithoutExtension (filePath);
  104. Logger.LogInformation ($"[{className}] [{caller}] {message}");
  105. }
  106. /// <summary>
  107. /// Logs a trace/verbose message including the class and method name.
  108. /// </summary>
  109. /// <param name="message"></param>
  110. /// <param name="caller"></param>
  111. /// <param name="filePath"></param>
  112. public static void Trace (
  113. string message,
  114. [CallerMemberName] string caller = "",
  115. [CallerFilePath] string filePath = ""
  116. )
  117. {
  118. string className = Path.GetFileNameWithoutExtension (filePath);
  119. Logger.LogTrace ($"[{className}] [{caller}] {message}");
  120. }
  121. /// <summary>
  122. /// Logs a warning message including the class and method name.
  123. /// </summary>
  124. /// <param name="message"></param>
  125. /// <param name="caller"></param>
  126. /// <param name="filePath"></param>
  127. public static void Warning (
  128. string message,
  129. [CallerMemberName] string caller = "",
  130. [CallerFilePath] string filePath = ""
  131. )
  132. {
  133. string className = Path.GetFileNameWithoutExtension (filePath);
  134. Logger.LogWarning ($"[{className}] [{caller}] {message}");
  135. }
  136. }