RE: Log an Exception

"Nicko Cadell" <[email protected]> Wed, 15 Dec 2004 17:10:13 -0000
Newsgroups gmane.comp.windows.dotnet.log4net.devel
Message-ID <[email protected]>
Mike,

> I am using the second method - Debug(object,Exception). =20

Ok good.

> Using PatternLayout's %m conversion character, the Exception=20
> does appear to be appended to the output of the pattern. =20

The %m will output the rendering of the message object, whatever that
is.
As you have specified an Exception and the PatternLayout hasn't output
it (it doesn't know how to) then the appender will output the Exception
following the message line.


> But, it doesn't appear to be using the Exception's ToString()=20
> method.  For example, with Log4NET, the Exception appears like this:
> 	Exception: System.ApplicationException
> 	Message: go bucks
> 	Source: Log4NETTest
> 	   at Log4NETTest.Class1.Test() in
> d:\development\projects\testprojects\log4nettest\class1.cs:line 24
> 	   at Log4NETTest.Class1.Main(String[] args) in=20
> d:\development\projects\testprojects\log4nettest\class1.cs:lin
> e 52 But if I just call the ToString() method, I see=20
> something like this:
> 	System.ApplicationException: go bucks
>    		at Log4NETTest.Class1.Test() in
> d:\development\projects\testprojects\log4nettest\class1.cs:line 24
> 		at Log4NETTest.Class1.Main(String[] args) in=20
> d:\development\projects\testprojects\log4nettest\class1.cs:line 52
>=20
> Is Log4NET rendering the Exception differently, or am I=20
> totally off base here?

Yes, log4net does get in and try to render the exception. This is does
using an IObjectRenderer and can be overridden for specified types.
This behaviour has actually been changed after 1.2 beta 8 so that by
default the exception's ToString method is used. Typically the ToString
method should output more detailed information about the exception than
the generic data log4net was extracting. It will still be possible to
override the rendering for specific exceptions using an IObjectRenderer.


> If I wanted to log the Exception message apart from the=20
> message, I would need to create my own layout and parser,=20
> correct?  For example, derive my custom layout and parser=20
> from PatternLayout and PatternParser?

Yes. But you should consider using the latest version from CVS as in
this version it is much easier to create new patterns for the
PatternLayout and also there is a built-in ExceptionPatternConverter
which may do what you want.
=20
=20
> On a side note, should I post any future response to this=20
> thread on the new [email protected] list?  I=20
> noticed that the Sourceforge.net lists are closed.

It would probably be best to post to the [email protected]
list. I keep meaning to fully close down the sourceforge lists.

Cheers,
Nicko

>=20
>=20
> Thanks,
> Mike
>=20
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf=20
> Of Nicko Cadell
> Sent: Wednesday, December 15, 2004 8:21 AM
> To: [email protected]
> Subject: RE: [Log4net-devel] Log an Exception
>=20
> Mike,
>=20
> Are you only logging an exception object or are you logging=20
> some message in addition to the exception?
>=20
> The log4net ILog interface supports methods like:
>=20
> Debug(object)
> Debug(object,Exception)
>=20
> Are you using the first or second method?
> log4net makes a distinction between the message (which is an=20
> object) and the additional exception. Typically a message is=20
> logged and may have an optional exception attached. log4net=20
> stores and processes the exception separately to the message.=20
> Note that as the message is an object it may be an Exception=20
> instance, but log4net still treats this as the message=20
> object, not the additional exception data.
>=20
> The message is rendered using the PatternLayout, the=20
> exception is added to the output if the layout does not render it.
> The ExceptionLayout takes the optional exception, not the=20
> message, and rendered that. It is only really used by the=20
> AdoNetAppender which uses multiple layouts.
>=20
> The PatternLayout's %m pattern renders the message object to a string.
> The PatternLayout does not process any additional exception=20
> that may have been specified. If there is an additional=20
> exception its ToString method is called an that is appended=20
> to the output of the pattern.
>=20
> Whether the exception is passed as the message or additional=20
> exception the object is not broken up into component parts -=20
> type, message, stack trace, inner exception. These parts=20
> cannot be logged separately using the built-in Layouts.
>=20
> Nicko
>=20
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]] On Behalf Of=20
> > Collier, Mike
> > Sent: 13 December 2004 23:42
> > To: [email protected];
> > [email protected]
> > Subject: [Log4net-devel] Log an Exception
> >=20
> > How would I log an exception (type, message, and stack=20
> trace) using,=20
> > for example, the ConsoleAppender or FileAppender?  It seems=20
> that the=20
> > "%m"
> > conversion character in PatternLayout prints my log=20
> message, and the=20
> > exception type and message, but not the stack trace.
> > I see that the ExceptionLayout prints out the exception=20
> information. =20
> > I don't see a way to use the ExceptionLayout with appenders=20
> like the=20
> > ConsoleAppender.  I could use the ExceptionLayout with the=20
> > ADONetAppender though.
> >=20
> > Do I need to implement my own conversion character?
> >=20
> > I'm using Log4NET 1.2.0 beta 8.
> >=20
> >=20
> > Thanks!
> >=20
> >=20
> >=20
> > -------------------------------------------------------
> > SF email is sponsored by - The IT Product Guide Read honest=20
> & candid=20
> > reviews on hundreds of IT Products from real users.
> > Discover which products truly live up to the hype. Start=20
> reading now.=20
> > http://productguide.itmanagersjournal.com/
> > _______________________________________________
> > Log4net-devel mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/log4net-devel
> >=20
>=20
>=20
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide Read honest &=20
> candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.=20
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Log4net-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/log4net-devel
>=20
>=20
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide Read honest &=20
> candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.=20
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Log4net-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/log4net-devel
>=20


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/