logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Duncan Woods" <duncan.wo...@garradhassan.com>
Subject RE: When to log, when to throw - Help!
Date Fri, 28 Jul 2006 08:38:47 GMT
Hi Jiho,

That's pretty much what I implemented which is to record the deepest
context reached as well as the normal NDC behaviour and declunking by
wrapping in my own function e.g.

	using (Context.Push("woo"))
	{
	}

the one weakness is that:

	using (Context.Push("woo"))
	{
	}
	throw new Exception("misleading context!");

means that the exception is thrown with misleading context, it will
still think it was 'woo'. It therefore becomes a coding standards issue
to never have code at the same level of scope as an applied context
although other context is fine like:

	using (Context.Push("woo"))
	{
	}
	using (Context.Push("wee"))
	{
	}

Thanks,
Duncan






-----Original Message-----
From: Jiho Han [mailto:jhan@InfinityInfo.com] 
Sent: 27 July 2006 14:34
To: Log4NET User
Subject: RE: When to log, when to throw - Help!

Hopefully, this will be threaded properly - I couldn't figure out how to
retrieve old messages from the list server.

I found this thread from MARC, and it got me interested because I was
looking for information on how to utilize NDC.  For the life of me, I
couldn't think of what NDC was for and how to use it.

Anyhow, how about you do this for an interim solution for the NDC
popping issue when an exception is thrown:
=====
 try
 {
  foreach (int oddNumber in oddNumbers)
  {
   using(NDC.Push("oddNumber: " + oddNumber))
   {
	log4net.ThreadContext.Properties["NDC2"] = NDC.ToString();
    foreach (int evenNumber in evenNumbers)
    {
     using(NDC.Push("evenNumber: " + evenNumber))
     {
	log4net.ThreadContext.Properties["NDC2"] = NDC.ToString();
      if (oddNumber == 7)
      {
       throw new ApplicationException("Oh no!");
      }
      log.DebugFormat("Sum: {0}", 
       oddNumber + evenNumber);
     }
    }
   }
  }
 }
 catch (Exception ex)
 {
  log.Fatal(
   "Unhandled exception. Cause: " + ex.Message, ex);
 }
=====
So, the idea is to put a copy of the stack into another Name/Value pair
every time you push.  I know it's clunky but it's better than forgetting
to pop by replacing using statements with a try..catch?  I thought about
using

log4net.ThreadContext.Properties["NDC2"] = NDC.CloneStack();

but, it seems to make a shallow copy.

The better solution would be for the log4net to make a snapshot of the
stack every time it's pushed, so that in our situation, even when
everything is popped, there remains the last pushed snapshot, if you
will.

NDC.LastPushState (for lack of better term)

Will always return the last pushed snapshot(instance of
ThreadContextStack) of the stack.

Jiho Han
Senior Software Engineer
Infinity Info Systems
The Sales Technology Experts
Tel: 212.563.4400 x216
Fax: 212.760.0540
jhan@infinityinfo.com
www.infinityinfo.com


Mime
View raw message