AspNetTraceAppender.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // .NET Compact Framework 1.0 has no support for ASP.NET
  20. // SSCLI 1.0 has no support for ASP.NET
  21. #if !NETCF && !SSCLI && !CLIENT_PROFILE
  22. using System.Web;
  23. using log4net.Layout;
  24. using log4net.Core;
  25. namespace log4net.Appender
  26. {
  27. /// <summary>
  28. /// <para>
  29. /// Appends log events to the ASP.NET <see cref="TraceContext"/> system.
  30. /// </para>
  31. /// </summary>
  32. /// <remarks>
  33. /// <para>
  34. /// Diagnostic information and tracing messages that you specify are appended to the output
  35. /// of the page that is sent to the requesting browser. Optionally, you can view this information
  36. /// from a separate trace viewer (Trace.axd) that displays trace information for every page in a
  37. /// given application.
  38. /// </para>
  39. /// <para>
  40. /// Trace statements are processed and displayed only when tracing is enabled. You can control
  41. /// whether tracing is displayed to a page, to the trace viewer, or both.
  42. /// </para>
  43. /// <para>
  44. /// The logging event is passed to the <see cref="M:TraceContext.Write(string)"/> or
  45. /// <see cref="M:TraceContext.Warn(string)"/> method depending on the level of the logging event.
  46. /// The event's logger name is the default value for the category parameter of the Write/Warn method.
  47. /// </para>
  48. /// </remarks>
  49. /// <author>Nicko Cadell</author>
  50. /// <author>Gert Driesen</author>
  51. /// <author>Ron Grabowski</author>
  52. public class AspNetTraceAppender : AppenderSkeleton
  53. {
  54. #region Public Instances Constructors
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="AspNetTraceAppender" /> class.
  57. /// </summary>
  58. /// <remarks>
  59. /// <para>
  60. /// Default constructor.
  61. /// </para>
  62. /// </remarks>
  63. public AspNetTraceAppender()
  64. {
  65. }
  66. #endregion // Public Instances Constructors
  67. #region Override implementation of AppenderSkeleton
  68. /// <summary>
  69. /// Write the logging event to the ASP.NET trace
  70. /// </summary>
  71. /// <param name="loggingEvent">the event to log</param>
  72. /// <remarks>
  73. /// <para>
  74. /// Write the logging event to the ASP.NET trace
  75. /// <c>HttpContext.Current.Trace</c>
  76. /// (<see cref="TraceContext"/>).
  77. /// </para>
  78. /// </remarks>
  79. override protected void Append(LoggingEvent loggingEvent)
  80. {
  81. // check if log4net is running in the context of an ASP.NET application
  82. if (HttpContext.Current != null)
  83. {
  84. // check if tracing is enabled for the current context
  85. if (HttpContext.Current.Trace.IsEnabled)
  86. {
  87. if (loggingEvent.Level >= Level.Warn)
  88. {
  89. HttpContext.Current.Trace.Warn(m_category.Format(loggingEvent), RenderLoggingEvent(loggingEvent));
  90. }
  91. else
  92. {
  93. HttpContext.Current.Trace.Write(m_category.Format(loggingEvent), RenderLoggingEvent(loggingEvent));
  94. }
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// This appender requires a <see cref="Layout"/> to be set.
  100. /// </summary>
  101. /// <value><c>true</c></value>
  102. /// <remarks>
  103. /// <para>
  104. /// This appender requires a <see cref="Layout"/> to be set.
  105. /// </para>
  106. /// </remarks>
  107. override protected bool RequiresLayout
  108. {
  109. get { return true; }
  110. }
  111. #endregion // Override implementation of AppenderSkeleton
  112. #region Public Instance Properties
  113. /// <summary>
  114. /// The category parameter sent to the Trace method.
  115. /// </summary>
  116. /// <remarks>
  117. /// <para>
  118. /// Defaults to %logger which will use the logger name of the current
  119. /// <see cref="LoggingEvent"/> as the category parameter.
  120. /// </para>
  121. /// <para>
  122. /// </para>
  123. /// </remarks>
  124. public PatternLayout Category
  125. {
  126. get { return m_category; }
  127. set { m_category = value; }
  128. }
  129. #endregion
  130. #region Private Instance Fields
  131. /// <summary>
  132. /// Defaults to %logger
  133. /// </summary>
  134. private PatternLayout m_category = new PatternLayout("%logger");
  135. #endregion
  136. }
  137. }
  138. #endif // !NETCF && !SSCLI