You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Todd <to...@yahoo.com> on 2011/10/19 20:13:12 UTC

Did the exception format change from XML -> string in 1.2.11?

Hello,
 
I've been using log4net for a few years now. I wrapped it my own library and forgot all about the internals until now ...
 
I grabbed the latest version and now I can't log any exceptions, APPARENTLY because the format changed. My sproc expects the xml payload, but I now seem to be getting a formatted string. The stacktrace in the string can contain "<>", which causes the call to the sproc to fail. (it's expecting xml data, but only sees xml brackets inside)
 
An example of a logged exception using 1.2.10:
 
<System.NullReferenceException>
  <Message>Object reference not set to an in
  <StackTrace>
    <Frame>MyCompany.Web.CMS.ContentReposito ...
    <Frame>MyCompany.Web.CMS.Repository.Fron ... 
    <Frame>MyCompany.Web.CMS.ContentReposito ...
    <Frame>MyCompany.Web.Corporate.Services. ...
  </StackTrace>
  <Data>
    <hostAddress>66.249.67.244</hostAddress>
  </Data>
</System.NullReferenceException>

An example of what log4net is trying to pass in with 1.2.11:
 
at MyCompany.Web.Corporate.Controllers.
C:\dev\IQ\Tfs2010\ExternalWeb\Corporate ...
at lambda_method(Closure , ControllerBa ...
at System.Web.Mvc.ActionMethodDispatche ...
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvok ...
at System.Web.Mvc.ControllerActionInvok ...

Can anyone help me wrap my brain around why this is happening and a possible solution? 
 
Thank you.

Re: Did the exception format change from XML -> string in 1.2.11?

Posted by Todd <to...@yahoo.com>.
Hi again, Roy,
 
IRawLayout is just an interface, and thus has no code. The only escaping that I could find was in the xml layout. And even that doesn't replace "<>".
 
So I came up with a lousy workaround today. I think there's got to be a better way, but what I did was clone the ExceptionLayout and add my own escaping.
 
As I didn't know how to escape AND use a pattern, I just combined it all. Ugly. I know. But I just couldn't find another way. At least not without studying the log4net infrastructure for a week to gain a complete understanding. I'm sure it's awesome, but it's not trivial.
 
    override public void Format(TextWriter writer, LoggingEvent loggingEvent) 
    {
      if (loggingEvent == null)
      {
        throw new ArgumentNullException("loggingEvent");
      }
            Regex invalidCharacters = new Regex(@"[<>&]", RegexOptions.Compiled);
            string data = invalidCharacters.Replace(loggingEvent.GetExceptionString(), "?");
            if (!string.IsNullOrEmpty(data))
            {
                data = string.Format("<DETAILS><EXCEPTION>{0}</EXCEPTION></DETAILS>", data);
            }
            writer.Write(data);
    }
 
I tried several layouts, as mentioned below and couldn't find a way to accomplish what I wanted. So now I'm stuck with a custom copy of log4net. :(
 

________________________________
 From: Roy Chastain <Ro...@roychastain.org>
To: Log4NET User <lo...@logging.apache.org>; Todd <to...@yahoo.com> 
Sent: Friday, December 16, 2011 4:44 PM
Subject: RE: Did the exception format change from XML -> string in 1.2.11?
 
Todd,
The <> that you are getting are because the class/method name has <> in
it.  It has these brackets because it is either a .NET Framework generic
class or an anonymous delegate/lambda expression.

Stephan said that he looked at the code and he believes that IRawLayout
in AdoNetAppender will correctly escape the <> as &lt; &gt;.

Can you confirm what layout you are using?

----------------------------------------------------------------------
Roy Chastain




-----Original Message-----
From: Todd [mailto:todd_beaulieu@yahoo.com] 
Sent: Friday, December 16, 2011 11:54
To: Log4NET User
Subject: Re: Did the exception format change from XML -> string in
1.2.11?

I'm back on this project and still struggling. I've spent HOURS trying
to figure this out.

Perhaps I'm going about this all wrong, I don't know. I'd be
appreciative if someone could tell me if I'm off base here, or what.

Goal:

I want to record the entire exception (stack trace is the mother load,
of course) in my database. I'm using a stored proc to insert the event
and that sproc has a Details parameter which is XML. 


What I've tried:

I've been able to specify a pattern that has the required XML wrapper
nodes (and no actual insertions from log4net) and that makes it into the
db. This is the format that I need to adhere to support the dashboard
that we use to monitor for problems.

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="&lt;DETAILS&gt;
&lt;EXCEPTION&gt;&lt;/EXCEPTION&gt; &lt;/DETAILS&gt;" /> </layout>

So this simply inserts:

<DETAILS>
  <EXCEPTION />
</DETAILS>

Next step, I tried to insert the exception itself by include
"%exception" in the above pattern at the appropriate place. 

I'm breakpointing on virtual protected void SendBuffer(IDbTransaction
dbTran, LoggingEvent[] events) just before it attempts to execute the
command. The exception is not being escaped. It contains <> in the
stacktrace, which blow up the XML. There may be other characters in
there as well, but hopefully the solution will resolve everything.

I tried the ExceptionLayout pattern. It doesn't escape the stacktrack.

I tried the above pattern with the XmlLayout and, even though I didn't
embed any field patterns, still got the exception, but wrapped in
<log4net:event> nodes. The inner text was still not escaped, and thus
failed.

So am I missing something obvious? Has anyone ever logged exceptions
using an XML data type? 

Thank you for any guidance!

From: Stefan Bodewig <bo...@apache.org>
To: Log4NET User <lo...@logging.apache.org>
Sent: Friday, October 21, 2011 6:20 AM
Subject: Re: Did the exception format change from XML -> string in
1.2.11?

On 2011-10-19, Todd wrote:

> I grabbed the latest version and now I can't log any exceptions, 
> APPARENTLY because the format changed. My sproc expects the xml 
> payload, but I now seem to be getting a formatted string. The 
> stacktrace in the string can contain "<>", which causes the call to 
> the sproc to fail. (it's expecting xml data, but only sees xml 
> brackets inside)

The actual formatting happens via the IRawLayout in AdoNetAppender (this
is what I assume you are using).  What does you configuration for this
look like?

I quickly glanced over the code changes in AdoNetAppender and the
related layout and converter classes but don't see any change that would
explain your findings.  Of course I may be missing something.

Stefan

RE: Did the exception format change from XML -> string in 1.2.11?

Posted by Roy Chastain <Ro...@roychastain.org>.
Todd,
The <> that you are getting are because the class/method name has <> in
it.  It has these brackets because it is either a .NET Framework generic
class or an anonymous delegate/lambda expression.

Stephan said that he looked at the code and he believes that IRawLayout
in AdoNetAppender will correctly escape the <> as &lt; &gt;.

Can you confirm what layout you are using?

----------------------------------------------------------------------
Roy Chastain




-----Original Message-----
From: Todd [mailto:todd_beaulieu@yahoo.com] 
Sent: Friday, December 16, 2011 11:54
To: Log4NET User
Subject: Re: Did the exception format change from XML -> string in
1.2.11?

I'm back on this project and still struggling. I've spent HOURS trying
to figure this out.
 
Perhaps I'm going about this all wrong, I don't know. I'd be
appreciative if someone could tell me if I'm off base here, or what.
 
Goal:
 
I want to record the entire exception (stack trace is the mother load,
of course) in my database. I'm using a stored proc to insert the event
and that sproc has a Details parameter which is XML. 
 
 
What I've tried:
 
I've been able to specify a pattern that has the required XML wrapper
nodes (and no actual insertions from log4net) and that makes it into the
db. This is the format that I need to adhere to support the dashboard
that we use to monitor for problems.
 
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="&lt;DETAILS&gt;
&lt;EXCEPTION&gt;&lt;/EXCEPTION&gt; &lt;/DETAILS&gt;" /> </layout>

So this simply inserts:
 
<DETAILS>
  <EXCEPTION />
</DETAILS>
 
Next step, I tried to insert the exception itself by include
"%exception" in the above pattern at the appropriate place. 
 
I'm breakpointing on virtual protected void SendBuffer(IDbTransaction
dbTran, LoggingEvent[] events) just before it attempts to execute the
command. The exception is not being escaped. It contains <> in the
stacktrace, which blow up the XML. There may be other characters in
there as well, but hopefully the solution will resolve everything.
 
I tried the ExceptionLayout pattern. It doesn't escape the stacktrack.
 
I tried the above pattern with the XmlLayout and, even though I didn't
embed any field patterns, still got the exception, but wrapped in
<log4net:event> nodes. The inner text was still not escaped, and thus
failed.
 
So am I missing something obvious? Has anyone ever logged exceptions
using an XML data type? 
 
Thank you for any guidance!

From: Stefan Bodewig <bo...@apache.org>
To: Log4NET User <lo...@logging.apache.org>
Sent: Friday, October 21, 2011 6:20 AM
Subject: Re: Did the exception format change from XML -> string in
1.2.11?

On 2011-10-19, Todd wrote:

> I grabbed the latest version and now I can't log any exceptions, 
> APPARENTLY because the format changed. My sproc expects the xml 
> payload, but I now seem to be getting a formatted string. The 
> stacktrace in the string can contain "<>", which causes the call to 
> the sproc to fail. (it's expecting xml data, but only sees xml 
> brackets inside)

The actual formatting happens via the IRawLayout in AdoNetAppender (this
is what I assume you are using).  What does you configuration for this
look like?

I quickly glanced over the code changes in AdoNetAppender and the
related layout and converter classes but don't see any change that would
explain your findings.  Of course I may be missing something.

Stefan




Re: Did the exception format change from XML -> string in 1.2.11?

Posted by Todd <to...@yahoo.com>.
I'm back on this project and still struggling. I've spent HOURS trying to figure this out.
 
Perhaps I'm going about this all wrong, I don't know. I'd be appreciative if someone could tell me if I'm off base here, or what.
 
Goal:
 
I want to record the entire exception (stack trace is the mother load, of course) in my database. I'm using a stored proc to insert the event and that sproc has a Details parameter which is XML. 
 
 
What I've tried:
 
I've been able to specify a pattern that has the required XML wrapper nodes (and no actual insertions from log4net) and that makes it into the db. This is the format that I need to adhere to support the dashboard that we use to monitor for problems.
 
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="&lt;DETAILS&gt; &lt;EXCEPTION&gt;&lt;/EXCEPTION&gt; &lt;/DETAILS&gt;" />
</layout>

So this simply inserts:
 
<DETAILS>
  <EXCEPTION />
</DETAILS>
 
Next step, I tried to insert the exception itself by include "%exception" in the above pattern at the appropriate place. 
 
I'm breakpointing on virtual protected void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) just before it attempts to execute the command. The exception is not being escaped. It contains <> in the stacktrace, which blow up the XML. There may be other characters in there as well, but hopefully the solution will resolve everything.
 
I tried the ExceptionLayout pattern. It doesn't escape the stacktrack.
 
I tried the above pattern with the XmlLayout and, even though I didn't embed any field patterns, still got the exception, but wrapped in <log4net:event> nodes. The inner text was still not escaped, and thus failed.
 
So am I missing something obvious? Has anyone ever logged exceptions using an XML data type? 
 
Thank you for any guidance!
 

________________________________
 From: Stefan Bodewig <bo...@apache.org>
To: Log4NET User <lo...@logging.apache.org> 
Sent: Friday, October 21, 2011 6:20 AM
Subject: Re: Did the exception format change from XML -> string in 1.2.11?
 
On 2011-10-19, Todd wrote:

> I grabbed the latest version and now I can't log any exceptions,
> APPARENTLY because the format changed. My sproc expects the xml
> payload, but I now seem to be getting a formatted string. The
> stacktrace in the string can contain "<>", which causes the call to
> the sproc to fail. (it's expecting xml data, but only sees xml
> brackets inside)

The actual formatting happens via the IRawLayout in AdoNetAppender (this
is what I assume you are using).  What does you configuration for this
look like?

I quickly glanced over the code changes in AdoNetAppender and the
related layout and converter classes but don't see any change that would
explain your findings.  Of course I may be missing something.

Stefan

Re: Did the exception format change from XML -> string in 1.2.11?

Posted by Stefan Bodewig <bo...@apache.org>.
On 2011-10-21, Stefan Bodewig wrote:

> On 2011-10-19, Todd wrote:

>> I grabbed the latest version and now I can't log any exceptions,
>> APPARENTLY because the format changed. My sproc expects the xml
>> payload, but I now seem to be getting a formatted string. The
>> stacktrace in the string can contain "<>", which causes the call to
>> the sproc to fail. (it's expecting xml data, but only sees xml
>> brackets inside)

> The actual formatting happens via the IRawLayout in AdoNetAppender (this
> is what I assume you are using).  What does you configuration for this
> look like?

As a side-note.  I have confirmed that XmlLayout by itself properly
escapes any <> characters that are part of the stack trace:
<https://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Layout/XmlLayoutTest.cs?r1=1187391&r2=1187390&pathrev=1187391>

The stack trace format looks completely different from the XML you've
posted in one of your posts, though.  So I suspect some other kind of
layout is in use for your case.

Stefan

Re: Did the exception format change from XML -> string in 1.2.11?

Posted by Stefan Bodewig <bo...@apache.org>.
On 2011-10-19, Todd wrote:

> I grabbed the latest version and now I can't log any exceptions,
> APPARENTLY because the format changed. My sproc expects the xml
> payload, but I now seem to be getting a formatted string. The
> stacktrace in the string can contain "<>", which causes the call to
> the sproc to fail. (it's expecting xml data, but only sees xml
> brackets inside)

The actual formatting happens via the IRawLayout in AdoNetAppender (this
is what I assume you are using).  What does you configuration for this
look like?

I quickly glanced over the code changes in AdoNetAppender and the
related layout and converter classes but don't see any change that would
explain your findings.  Of course I may be missing something.

Stefan

Re: Did the exception format change from XML -> string in 1.2.11?

Posted by Todd <to...@yahoo.com>.
Thanks for trying to help, Roy.
 
I don't know what you mean by "generic classes".
 
I had exceptions logging for years now with the exception making it to MY simple sproc that takes the exception param as an XML sql data type.
 
Suddenly it stopped working in development. Anything with a stack trace that had <> symbols in it would fail to log. This would fail in the sqldata lib because it's not an XML format, of course.
 
I tried backing out to the old log4net dll and I'm still getting the failure. Now I have no idea what is going on. I'm sure log4net is a wonderful architecture, but I find it difficult to just jump in and debug. I suspect it's something one needs to really study for a while to get a handle on the architecture. I've never dug very deep.
 
I thought the xml representation (serialization) of the exception was being done for in previously.
 
I just tried to change that log4net param binding to "log4net.Layout.ExceptionLayout" (total guess, frankly) and now I noticed this exception in the console output:
 log4net: Setting Property [ParameterName] to String value [@details]
log4net: Setting Property [DbType] to DbType value [String]
log4net: Setting Property [Size] to Int32 value [4000]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [conversionPattern] to set object on [log4net.Layout.ExceptionLayout]
log4net: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter]
log4net: reated Appender [SQLAppender]
log4net: Adding appender named [SQLAppender] to logger [root].
<appender name="SQLAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <!-- connection string is set later -->
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

  <!-- SystemName and SystemTag params are added dynamically and set by the calling application -->
  <commandText value="EXEC log.RecordEvent @SystemName=@system_name , @SystemTag=@system_tag , @Severity=@log_level , @Description=@message , @Details=@details" />
 
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="5" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-level" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@details" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.ExceptionLayout">
      <conversionPattern value="&lt;DETAILS&gt; &lt;EXCEPTION&gt;%exception&lt;/EXCEPTION&gt; &lt;/DETAILS&gt;" />
    </layout>
  </parameter>
</appender>



________________________________
From: Roy Chastain <Ro...@roychastain.org>
To: Log4NET User <lo...@logging.apache.org>; Todd <to...@yahoo.com>
Sent: Wednesday, October 19, 2011 6:55 PM
Subject: RE: Did the exception format change from XML -> string in 1.2.11?

Looks like generic classes to me.
What "sproc" are you referring to?

----------------------------------------------------------------------
Roy Chastain




-----Original Message-----
From: Todd [mailto:todd_beaulieu@yahoo.com] 
Sent: Wednesday, October 19, 2011 14:13
To: log4net-user@logging.apache.org
Subject: Did the exception format change from XML -> string in 1.2.11?

Hello,

I've been using log4net for a few years now. I wrapped it my own library
and forgot all about the internals until now ...

I grabbed the latest version and now I can't log any exceptions,
APPARENTLY because the format changed. My sproc expects the xml payload,
but I now seem to be getting a formatted string. The stacktrace in the
string can contain "<>", which causes the call to the sproc to fail.
(it's expecting xml data, but only sees xml brackets inside)

An example of a logged exception using 1.2.10:

<System.NullReferenceException>
  <Message>Object reference not set to an in
  <StackTrace>
    <Frame>MyCompany.Web.CMS.ContentReposito ...
    <Frame>MyCompany.Web.CMS.Repository.Fron ... 
    <Frame>MyCompany.Web.CMS.ContentReposito ...
    <Frame>MyCompany.Web.Corporate.Services. ...
  </StackTrace>
  <Data>
    <hostAddress>66.249.67.244</hostAddress>
  </Data>
</System.NullReferenceException>

An example of what log4net is trying to pass in with 1.2.11:

at MyCompany.Web.Corporate.Controllers.
C:\dev\IQ\Tfs2010\ExternalWeb\Corporate ...
at lambda_method(Closure , ControllerBa ...
at System.Web.Mvc.ActionMethodDispatche ...
at
System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeAction
MethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvok ...
at System.Web.Mvc.ControllerActionInvok ...

Can anyone help me wrap my brain around why this is happening and a
possible solution? 

Thank you.

RE: Did the exception format change from XML -> string in 1.2.11?

Posted by Roy Chastain <Ro...@roychastain.org>.
Looks like generic classes to me.
What "sproc" are you referring to?

----------------------------------------------------------------------
Roy Chastain




-----Original Message-----
From: Todd [mailto:todd_beaulieu@yahoo.com] 
Sent: Wednesday, October 19, 2011 14:13
To: log4net-user@logging.apache.org
Subject: Did the exception format change from XML -> string in 1.2.11?

Hello,
 
I've been using log4net for a few years now. I wrapped it my own library
and forgot all about the internals until now ...
 
I grabbed the latest version and now I can't log any exceptions,
APPARENTLY because the format changed. My sproc expects the xml payload,
but I now seem to be getting a formatted string. The stacktrace in the
string can contain "<>", which causes the call to the sproc to fail.
(it's expecting xml data, but only sees xml brackets inside)
 
An example of a logged exception using 1.2.10:
 
<System.NullReferenceException>
  <Message>Object reference not set to an in
  <StackTrace>
    <Frame>MyCompany.Web.CMS.ContentReposito ...
    <Frame>MyCompany.Web.CMS.Repository.Fron ... 
    <Frame>MyCompany.Web.CMS.ContentReposito ...
    <Frame>MyCompany.Web.Corporate.Services. ...
  </StackTrace>
  <Data>
    <hostAddress>66.249.67.244</hostAddress>
  </Data>
</System.NullReferenceException>

An example of what log4net is trying to pass in with 1.2.11:
 
at MyCompany.Web.Corporate.Controllers.
C:\dev\IQ\Tfs2010\ExternalWeb\Corporate ...
at lambda_method(Closure , ControllerBa ...
at System.Web.Mvc.ActionMethodDispatche ...
at
System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeAction
MethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvok ...
at System.Web.Mvc.ControllerActionInvok ...

Can anyone help me wrap my brain around why this is happening and a
possible solution? 
 
Thank you.