ConsoleAppender.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Globalization;
  21. using log4net.Layout;
  22. using log4net.Core;
  23. using log4net.Util;
  24. namespace log4net.Appender
  25. {
  26. /// <summary>
  27. /// Appends logging events to the console.
  28. /// </summary>
  29. /// <remarks>
  30. /// <para>
  31. /// ConsoleAppender appends log events to the standard output stream
  32. /// or the error output stream using a layout specified by the
  33. /// user.
  34. /// </para>
  35. /// <para>
  36. /// By default, all output is written to the console's standard output stream.
  37. /// The <see cref="Target"/> property can be set to direct the output to the
  38. /// error stream.
  39. /// </para>
  40. /// <para>
  41. /// NOTE: This appender writes each message to the <c>System.Console.Out</c> or
  42. /// <c>System.Console.Error</c> that is set at the time the event is appended.
  43. /// Therefore it is possible to programmatically redirect the output of this appender
  44. /// (for example NUnit does this to capture program output). While this is the desired
  45. /// behavior of this appender it may have security implications in your application.
  46. /// </para>
  47. /// </remarks>
  48. /// <author>Nicko Cadell</author>
  49. /// <author>Gert Driesen</author>
  50. public class ConsoleAppender : AppenderSkeleton
  51. {
  52. #region Public Instance Constructors
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="ConsoleAppender" /> class.
  55. /// </summary>
  56. /// <remarks>
  57. /// The instance of the <see cref="ConsoleAppender" /> class is set up to write
  58. /// to the standard output stream.
  59. /// </remarks>
  60. public ConsoleAppender()
  61. {
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="ConsoleAppender" /> class
  65. /// with the specified layout.
  66. /// </summary>
  67. /// <param name="layout">the layout to use for this appender</param>
  68. /// <remarks>
  69. /// The instance of the <see cref="ConsoleAppender" /> class is set up to write
  70. /// to the standard output stream.
  71. /// </remarks>
  72. [Obsolete("Instead use the default constructor and set the Layout property")]
  73. public ConsoleAppender(ILayout layout) : this(layout, false)
  74. {
  75. }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="ConsoleAppender" /> class
  78. /// with the specified layout.
  79. /// </summary>
  80. /// <param name="layout">the layout to use for this appender</param>
  81. /// <param name="writeToErrorStream">flag set to <c>true</c> to write to the console error stream</param>
  82. /// <remarks>
  83. /// When <paramref name="writeToErrorStream" /> is set to <c>true</c>, output is written to
  84. /// the standard error output stream. Otherwise, output is written to the standard
  85. /// output stream.
  86. /// </remarks>
  87. [Obsolete("Instead use the default constructor and set the Layout & Target properties")]
  88. public ConsoleAppender(ILayout layout, bool writeToErrorStream)
  89. {
  90. Layout = layout;
  91. m_writeToErrorStream = writeToErrorStream;
  92. }
  93. #endregion Public Instance Constructors
  94. #region Public Instance Properties
  95. /// <summary>
  96. /// Target is the value of the console output stream.
  97. /// This is either <c>"Console.Out"</c> or <c>"Console.Error"</c>.
  98. /// </summary>
  99. /// <value>
  100. /// Target is the value of the console output stream.
  101. /// This is either <c>"Console.Out"</c> or <c>"Console.Error"</c>.
  102. /// </value>
  103. /// <remarks>
  104. /// <para>
  105. /// Target is the value of the console output stream.
  106. /// This is either <c>"Console.Out"</c> or <c>"Console.Error"</c>.
  107. /// </para>
  108. /// </remarks>
  109. virtual public string Target
  110. {
  111. get { return m_writeToErrorStream ? ConsoleError : ConsoleOut; }
  112. set
  113. {
  114. string v = value.Trim();
  115. if (SystemInfo.EqualsIgnoringCase(ConsoleError, v))
  116. {
  117. m_writeToErrorStream = true;
  118. }
  119. else
  120. {
  121. m_writeToErrorStream = false;
  122. }
  123. }
  124. }
  125. #endregion Public Instance Properties
  126. #region Override implementation of AppenderSkeleton
  127. /// <summary>
  128. /// This method is called by the <see cref="M:AppenderSkeleton.DoAppend(LoggingEvent)"/> method.
  129. /// </summary>
  130. /// <param name="loggingEvent">The event to log.</param>
  131. /// <remarks>
  132. /// <para>
  133. /// Writes the event to the console.
  134. /// </para>
  135. /// <para>
  136. /// The format of the output will depend on the appender's layout.
  137. /// </para>
  138. /// </remarks>
  139. override protected void Append(LoggingEvent loggingEvent)
  140. {
  141. #if NETCF_1_0
  142. // Write to the output stream
  143. Console.Write(RenderLoggingEvent(loggingEvent));
  144. #else
  145. if (m_writeToErrorStream)
  146. {
  147. // Write to the error stream
  148. Console.Error.Write(RenderLoggingEvent(loggingEvent));
  149. }
  150. else
  151. {
  152. // Write to the output stream
  153. Console.Write(RenderLoggingEvent(loggingEvent));
  154. }
  155. #endif
  156. }
  157. /// <summary>
  158. /// This appender requires a <see cref="Layout"/> to be set.
  159. /// </summary>
  160. /// <value><c>true</c></value>
  161. /// <remarks>
  162. /// <para>
  163. /// This appender requires a <see cref="Layout"/> to be set.
  164. /// </para>
  165. /// </remarks>
  166. override protected bool RequiresLayout
  167. {
  168. get { return true; }
  169. }
  170. #endregion Override implementation of AppenderSkeleton
  171. #region Public Static Fields
  172. /// <summary>
  173. /// The <see cref="ConsoleAppender.Target"/> to use when writing to the Console
  174. /// standard output stream.
  175. /// </summary>
  176. /// <remarks>
  177. /// <para>
  178. /// The <see cref="ConsoleAppender.Target"/> to use when writing to the Console
  179. /// standard output stream.
  180. /// </para>
  181. /// </remarks>
  182. public const string ConsoleOut = "Console.Out";
  183. /// <summary>
  184. /// The <see cref="ConsoleAppender.Target"/> to use when writing to the Console
  185. /// standard error output stream.
  186. /// </summary>
  187. /// <remarks>
  188. /// <para>
  189. /// The <see cref="ConsoleAppender.Target"/> to use when writing to the Console
  190. /// standard error output stream.
  191. /// </para>
  192. /// </remarks>
  193. public const string ConsoleError = "Console.Error";
  194. #endregion Public Static Fields
  195. #region Private Instances Fields
  196. private bool m_writeToErrorStream = false;
  197. #endregion Private Instances Fields
  198. }
  199. }