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 <[email protected]>
To: Log4NET User <[email protected]>; Todd
<[email protected]>
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 < >.
Can you confirm what layout you are using?
----------------------------------------------------------------------
Roy Chastain
-----Original Message-----
From: Todd [mailto:[email protected]]
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="<DETAILS>
<EXCEPTION></EXCEPTION> </DETAILS>" /> </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 <[email protected]>
To: Log4NET User <[email protected]>
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