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