TextWriterLogger.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Globalization;
  19. using System.IO;
  20. using Grpc.Core.Utils;
  21. namespace Grpc.Core.Logging
  22. {
  23. /// <summary>Logger that logs to an arbitrary <c>System.IO.TextWriter</c>.</summary>
  24. public class TextWriterLogger : ILogger
  25. {
  26. // Format similar enough to C core log format except nanosecond precision is not supported.
  27. const string DateTimeFormatString = "MMdd HH:mm:ss.ffffff";
  28. readonly Func<TextWriter> textWriterProvider;
  29. readonly Type forType;
  30. readonly string forTypeString;
  31. /// <summary>
  32. /// Creates a console logger not associated to any specific type and writes to given <c>System.IO.TextWriter</c>.
  33. /// User is responsible for providing an instance of TextWriter that is thread-safe.
  34. /// </summary>
  35. public TextWriterLogger(TextWriter textWriter) : this(() => textWriter)
  36. {
  37. GrpcPreconditions.CheckNotNull(textWriter);
  38. }
  39. /// <summary>
  40. /// Creates a console logger not associated to any specific type and writes to a <c>System.IO.TextWriter</c> obtained from given provider.
  41. /// User is responsible for providing an instance of TextWriter that is thread-safe.
  42. /// </summary>
  43. public TextWriterLogger(Func<TextWriter> textWriterProvider) : this(textWriterProvider, null)
  44. {
  45. }
  46. /// <summary>Creates a console logger that logs messsage specific for given type.</summary>
  47. protected TextWriterLogger(Func<TextWriter> textWriterProvider, Type forType)
  48. {
  49. this.textWriterProvider = GrpcPreconditions.CheckNotNull(textWriterProvider);
  50. this.forType = forType;
  51. if (forType != null)
  52. {
  53. var namespaceStr = forType.Namespace ?? "";
  54. if (namespaceStr.Length > 0)
  55. {
  56. namespaceStr += ".";
  57. }
  58. this.forTypeString = namespaceStr + forType.Name + " ";
  59. }
  60. else
  61. {
  62. this.forTypeString = "";
  63. }
  64. }
  65. /// <summary>
  66. /// Returns a logger associated with the specified type.
  67. /// </summary>
  68. public virtual ILogger ForType<T>()
  69. {
  70. if (typeof(T) == forType)
  71. {
  72. return this;
  73. }
  74. return new TextWriterLogger(this.textWriterProvider, typeof(T));
  75. }
  76. /// <summary>Logs a message with severity Debug.</summary>
  77. public void Debug(string message)
  78. {
  79. Log("D", message);
  80. }
  81. /// <summary>Logs a formatted message with severity Debug.</summary>
  82. public void Debug(string format, params object[] formatArgs)
  83. {
  84. Debug(string.Format(format, formatArgs));
  85. }
  86. /// <summary>Logs a message with severity Info.</summary>
  87. public void Info(string message)
  88. {
  89. Log("I", message);
  90. }
  91. /// <summary>Logs a formatted message with severity Info.</summary>
  92. public void Info(string format, params object[] formatArgs)
  93. {
  94. Info(string.Format(format, formatArgs));
  95. }
  96. /// <summary>Logs a message with severity Warning.</summary>
  97. public void Warning(string message)
  98. {
  99. Log("W", message);
  100. }
  101. /// <summary>Logs a formatted message with severity Warning.</summary>
  102. public void Warning(string format, params object[] formatArgs)
  103. {
  104. Warning(string.Format(format, formatArgs));
  105. }
  106. /// <summary>Logs a message and an associated exception with severity Warning.</summary>
  107. public void Warning(Exception exception, string message)
  108. {
  109. Warning(message + " " + exception);
  110. }
  111. /// <summary>Logs a message with severity Error.</summary>
  112. public void Error(string message)
  113. {
  114. Log("E", message);
  115. }
  116. /// <summary>Logs a formatted message with severity Error.</summary>
  117. public void Error(string format, params object[] formatArgs)
  118. {
  119. Error(string.Format(format, formatArgs));
  120. }
  121. /// <summary>Logs a message and an associated exception with severity Error.</summary>
  122. public void Error(Exception exception, string message)
  123. {
  124. Error(message + " " + exception);
  125. }
  126. /// <summary>Gets the type associated with this logger.</summary>
  127. protected Type AssociatedType
  128. {
  129. get { return forType; }
  130. }
  131. private void Log(string severityString, string message)
  132. {
  133. textWriterProvider().WriteLine("{0}{1} {2}{3}",
  134. severityString,
  135. DateTime.Now.ToString(DateTimeFormatString, CultureInfo.InvariantCulture),
  136. forTypeString,
  137. message);
  138. }
  139. }
  140. }