1

Let's say I have some application code like this:

public void DoSomething()
{
    if (SomethingBad)
        throw new SomethingBadException("You did something bad");

    // Keep going...
}

I'm using log4net, and ideally I'd like to log that exception, with the following caveats:

  • I'd like to log the exception at this point, and not depend on the caller to log it
  • I'd like to log the exception using a Logger owned by this class, so that the logger name is correct
  • I don't want SomethingBadException to know about logging, as sometimes I want to log this particular exception type, and sometimes I don't. I also want to use the same pattern for built-in exception types, such as NotSupportedException
  • I want to have a reusable pattern where the exception / logging statement is not too complex, as I'm going to have a lot of these. If this involves creating a utility class that does most of the work, this is fine.
  • I'd like to log the exception including the call stack, which means that it has to be logged after it is thrown. Logging it after the exception is constructed, but before it is thrown, gives me an empty call stack in the log output.

I've been able to write some utility code that accomplishes most of those bullets, but the last one involving capturing the call stack is eluding me.

Is there any way to accomplish all of these? I'm willing to write custom log4net extensions. I'm also using Ninject's log4net extension to build loggers if that helps.

Am I barking up the wrong tree? Is there another approach entirely that gets me where I want to go?

RationalGeek
  • 9,425
  • 11
  • 62
  • 90

2 Answers2

2

While looking for an easy way to remove the extra frame issue mentioned in the comments, I came across Environment.StackTrace. That might be a better fit for what you want.

Original answer follows.

Don't know log4net, but I would expect something like the following would do what you want.

void ThrowLoggedException<T>() where T : Exception
{
    try
    {
        throw new T("You did something bad");
    }
    catch (Exception ex)
    {
        // Log event here.
        throw;
    }
}
Martin Shobe
  • 146
  • 2
  • Note that this would add another entry to the call stack. If that's a problem you may need to manually inline it. Also need to add `where T : Exception`. – Servy Jan 24 '13 at 16:55
  • Something like this will probably work. I'll give it a shot. – RationalGeek Jan 24 '13 at 17:57
0

Use NLog - http://nlog-project.org/

In the more advanced Log4j style output layout it offers a lot of options about what data to log.

I use it in combination with Log2Console to view in real-time what is being logged.

dutzu
  • 3,883
  • 13
  • 19
  • 1
    Can you explain to me how NLog would be able to solve this specific problem better than log4net? I would consider switching loggers, but I find it hand to believe that NLog could solve this in a way that would be impossible with log4net. – RationalGeek Jan 24 '13 at 16:43
  • @RationalGeek - you can see there has been an extensive argument regarding this issue here http://stackoverflow.com/questions/710863/log4net-vs-nlog and decide what is best for you. – dutzu Jan 24 '13 at 16:47
  • @RationalGeek - Also check out http://www.dotnetlogging.com/comparison/ and http://www.alteridem.net/2010/11/04/why-i-switched-from-log4net-to-nlog/ Hope you find the right solution for you – dutzu Jan 24 '13 at 16:48