public string Create(LoggingEvent loggingEvent)
        {
            var data = new Dictionary<string, object>();

            data["level"] = loggingEvent.Level.ToString();
            data["machine"] = Environment.MachineName;
            data["message"] = loggingEvent.RenderedMessage;
            data["timestamp"] = ConvertDateTime(loggingEvent.TimeStamp);
            data["thread"] = loggingEvent.ThreadName;
            data["logger"] = loggingEvent.LoggerName;

            if (loggingEvent.LocationInformation != null)
            {
                data["file"] = loggingEvent.LocationInformation.FileName;
                data["line"] = loggingEvent.LocationInformation.LineNumber;
                data["method"] = loggingEvent.LocationInformation.MethodName;
                data["class"] = loggingEvent.LocationInformation.ClassName;
            }

            var exceptionString = loggingEvent.GetExceptionStrRep();

            if (!string.IsNullOrEmpty(exceptionString))
            {
                data["exception"] = exceptionString;
            }

            AddHttpUrl(data);
            AddHttpUser(data);
            AddHttpUrlReferrer(data);

            return _serializer.Serialize(data);
        }
예제 #2
0
        public override FilterDecision Decide(LoggingEvent loggingEvent)
        {
            if (loggingEvent == null)
                throw new ArgumentNullException("loggingEvent");

            var exceptionMessage = loggingEvent.GetExceptionStrRep();
            if (string.IsNullOrEmpty(exceptionMessage))
            {
                return FilterDecision.NEUTRAL;
            }

            if (RegexToMatch != null)
            {
                var regex = new Regex(RegexToMatch, RegexOptions.Multiline);

                if (!regex.Match(exceptionMessage).Success)
                    return FilterDecision.NEUTRAL;

                return AcceptOnMatch ? FilterDecision.ACCEPT : FilterDecision.DENY;
            }

            if (StringToMatch == null || exceptionMessage.IndexOf(StringToMatch) == -1)
            {
                return FilterDecision.NEUTRAL;
            }

            return AcceptOnMatch ? FilterDecision.ACCEPT : FilterDecision.DENY;
        }
		/// <summary>
		/// Gets the exception text from the logging event
		/// </summary>
		/// <param name="loggingEvent">the event being logged</param>
		/// <returns>the formatted string</returns>
		override public string Format(LoggingEvent loggingEvent) 
		{
			if (loggingEvent == null)
			{
				throw new ArgumentNullException("loggingEvent");
			}

			return loggingEvent.GetExceptionStrRep();
		}
		/* Example log4j schema event

<log4j:event logger="first logger" level="ERROR" thread="Thread-3" timestamp="1051494121460">
  <log4j:message><![CDATA[errormsg 3]]></log4j:message>
  <log4j:NDC><![CDATA[third]]></log4j:NDC>
  <log4j:MDC>
    <log4j:data name="some string" value="some valuethird"/>
  </log4j:MDC>
  <log4j:throwable><![CDATA[java.lang.Exception: someexception-third
 	at org.apache.log4j.chainsaw.Generator.run(Generator.java:94)
]]></log4j:throwable>
  <log4j:locationInfo class="org.apache.log4j.chainsaw.Generator"
method="run" file="Generator.java" line="94"/>
  <log4j:properties>
    <log4j:data name="log4jmachinename" value="windows"/>
    <log4j:data name="log4japp" value="udp-generator"/>
  </log4j:properties>
</log4j:event>

		*/

		/// <summary>
		/// Actualy do the writing of the xml
		/// </summary>
		/// <param name="writer">the writer to use</param>
		/// <param name="loggingEvent">the event to write</param>
		override protected void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
		{
			// Translate logging events for log4j

			// Translate hostname property
			if (loggingEvent.Properties[LoggingEvent.HostNameProperty] != null && 
				loggingEvent.Properties["log4jmachinename"] == null)
			{
				loggingEvent.Properties["log4jmachinename"] = loggingEvent.Properties[LoggingEvent.HostNameProperty];
			}

			// translate appdomain name
			if (loggingEvent.Properties["log4japp"] == null && 
				loggingEvent.Domain != null && 
				loggingEvent.Domain.Length > 0)
			{
				loggingEvent.Properties["log4japp"] = loggingEvent.Domain;
			}

			// translate identity name
			if (loggingEvent.Identity != null && 
				loggingEvent.Identity.Length > 0 && 
				loggingEvent.Properties[LoggingEvent.IdentityProperty] == null)
			{
				loggingEvent.Properties[LoggingEvent.IdentityProperty] = loggingEvent.Identity;
			}

			// translate user name
			if (loggingEvent.UserName != null && 
				loggingEvent.UserName.Length > 0 && 
				loggingEvent.Properties[LoggingEvent.UserNameProperty] == null)
			{
				loggingEvent.Properties[LoggingEvent.UserNameProperty] = loggingEvent.UserName;
			}

			// Write the start element
			writer.WriteStartElement("log4j:event");
			writer.WriteAttributeString("logger", loggingEvent.LoggerName);

			// Calculate the timestamp as the number of milliseconds since january 1970
			TimeSpan timeSince1970 = loggingEvent.TimeStamp - s_date1970;
			writer.WriteAttributeString("timestamp", XmlConvert.ToString((long)timeSince1970.TotalMilliseconds));
			writer.WriteAttributeString("level", loggingEvent.Level.ToString());
			writer.WriteAttributeString("thread", loggingEvent.ThreadName);
    
			// Append the message text
			writer.WriteStartElement("log4j:message");
			Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage);
			writer.WriteEndElement();

			if (loggingEvent.NestedContext != null && loggingEvent.NestedContext.Length > 0)
			{
				// Append the NDC text
				writer.WriteStartElement("log4j:NDC");
				Transform.WriteEscapedXmlString(writer, loggingEvent.NestedContext);
				writer.WriteEndElement();
			}

			if (loggingEvent.MappedContext != null && loggingEvent.MappedContext.Count > 0)
			{
				// Append the MDC text
				writer.WriteStartElement("log4j:MDC");
				foreach(System.Collections.DictionaryEntry entry in loggingEvent.MappedContext)
				{
					writer.WriteStartElement("log4j:data");
					writer.WriteAttributeString("name", entry.Key.ToString());
					writer.WriteAttributeString("value", entry.Value.ToString());
					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}

			if (loggingEvent.Properties != null)
			{
				// Append the properties text
				string[] propKeys = loggingEvent.Properties.GetKeys();
				if (propKeys.Length > 0)
				{
					writer.WriteStartElement("log4j:properties");
					foreach(string key in propKeys)
					{
						writer.WriteStartElement("log4j:data");
						writer.WriteAttributeString("name", key);
						writer.WriteAttributeString("value", loggingEvent.Properties[key].ToString());
						writer.WriteEndElement();
					}
					writer.WriteEndElement();
				}
			}

			string exceptionStr = loggingEvent.GetExceptionStrRep();
			if (exceptionStr != null && exceptionStr.Length > 0)
			{
				// Append the stack trace line
				writer.WriteStartElement("log4j:throwable");
				Transform.WriteEscapedXmlString(writer, exceptionStr);
				writer.WriteEndElement();
			}

			if (LocationInfo)
			{ 
				LocationInfo locationInfo = loggingEvent.LocationInformation;

				writer.WriteStartElement("log4j:locationInfo");
				writer.WriteAttributeString("class", locationInfo.ClassName);
				writer.WriteAttributeString("method", locationInfo.MethodName);
				writer.WriteAttributeString("file", locationInfo.FileName);
				writer.WriteAttributeString("line", locationInfo.LineNumber);
				writer.WriteEndElement();
			}

			writer.WriteEndElement();
		}
예제 #5
0
		/// <summary>
		/// Does the actual writing of the XML.
		/// </summary>
		/// <param name="writer">The writer to use to output the event to.</param>
		/// <param name="loggingEvent">The event to write.</param>
		override protected void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
		{
			writer.WriteStartElement(m_elmEvent);
			writer.WriteAttributeString(ATTR_LOGGER, loggingEvent.LoggerName);
			writer.WriteAttributeString(ATTR_TIMESTAMP, XmlConvert.ToString(loggingEvent.TimeStamp));
			writer.WriteAttributeString(ATTR_LEVEL, loggingEvent.Level.ToString());
			writer.WriteAttributeString(ATTR_THREAD, loggingEvent.ThreadName);

			if (loggingEvent.Domain != null && loggingEvent.Domain.Length > 0)
			{
				writer.WriteAttributeString(ATTR_DOMAIN, loggingEvent.Domain);
			}
			if (loggingEvent.Identity != null && loggingEvent.Identity.Length > 0)
			{
				writer.WriteAttributeString(ATTR_IDENTITY, loggingEvent.Identity);
			}
			if (loggingEvent.UserName != null && loggingEvent.UserName.Length > 0)
			{
				writer.WriteAttributeString(ATTR_USERNAME, loggingEvent.UserName);
			}
    
			// Append the message text
			writer.WriteStartElement(m_elmMessage);
			Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage);
			writer.WriteEndElement();

			if (loggingEvent.NestedContext != null && loggingEvent.NestedContext.Length > 0)
			{
				// Append the NDC text
				writer.WriteStartElement(m_elmNdc);
				Transform.WriteEscapedXmlString(writer, loggingEvent.NestedContext);
				writer.WriteEndElement();
			}

			if (loggingEvent.MappedContext != null && loggingEvent.MappedContext.Count > 0)
			{
				// Append the MDC text
				writer.WriteStartElement(m_elmMdc);
				foreach(System.Collections.DictionaryEntry entry in loggingEvent.MappedContext)
				{
					writer.WriteStartElement(m_elmData);
					writer.WriteAttributeString(ATTR_NAME, entry.Key.ToString());
					writer.WriteAttributeString(ATTR_VALUE, entry.Value.ToString());
					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}

			if (loggingEvent.Properties != null)
			{
				// Append the properties text
				string[] propKeys = loggingEvent.Properties.GetKeys();
				if (propKeys.Length > 0)
				{
					writer.WriteStartElement(m_elmProperties);
					foreach(string key in propKeys)
					{
						writer.WriteStartElement(m_elmData);
						writer.WriteAttributeString(ATTR_NAME, key);
						writer.WriteAttributeString(ATTR_VALUE, loggingEvent.Properties[key].ToString());
						writer.WriteEndElement();
					}
					writer.WriteEndElement();
				}
			}

			string exceptionStr = loggingEvent.GetExceptionStrRep();
			if (exceptionStr != null && exceptionStr.Length > 0)
			{
				// Append the stack trace line
				writer.WriteStartElement(m_elmException);
				Transform.WriteEscapedXmlString(writer, exceptionStr);
				writer.WriteEndElement();
			}

			if (LocationInfo)
			{ 
				LocationInfo locationInfo = loggingEvent.LocationInformation;

				writer.WriteStartElement(m_elmLocation);
				writer.WriteAttributeString(ATTR_CLASS, locationInfo.ClassName);
				writer.WriteAttributeString(ATTR_METHOD, locationInfo.MethodName);
				writer.WriteAttributeString(ATTR_FILE, locationInfo.FileName);
				writer.WriteAttributeString(ATTR_LINE, locationInfo.LineNumber);
				writer.WriteEndElement();
			}

			writer.WriteEndElement();
		}
		/// <summary>
		/// Renders the <see cref="LoggingEvent"/> to a string.
		/// </summary>
		/// <param name="loggingEvent">The event to render.</param>
		/// <returns>The event rendered as a string.</returns>
		/// <remarks>
		/// <para>
		/// Helper method to render a <see cref="LoggingEvent"/> to 
		/// a string. This appender must have a <see cref="Layout"/>
		/// set to render the <paramref name="loggingEvent"/> to 
		/// a string.
		/// </para>
		/// <para>If there is exception data in the logging event and 
		/// the layout does not process the exception, this method 
		/// will append the exception text to the rendered string.
		/// </para>
		/// </remarks>
		protected string RenderLoggingEvent(LoggingEvent loggingEvent) 
		{
			if (m_layout == null) 
			{
				throw new InvalidOperationException("A layout must be set");
			}

			if (m_layout.IgnoresException) 
			{
				string exceptionStr = loggingEvent.GetExceptionStrRep();
				if (exceptionStr != null && exceptionStr.Length > 0) 
				{
					// render the event and the exception
					return m_layout.Format(loggingEvent) + exceptionStr + SystemInfo.NewLine;
				}
				else 
				{
					// there is no exception to render
					return m_layout.Format(loggingEvent);
				}
			}
			else 
			{
				// The layout will render the exception
				return m_layout.Format(loggingEvent);
			}
		}