Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,971 | Comments: 44,508

filter by tags archive

How NOT to write a logger


I am going over some library code at the moment, and I am following my usual routine of starting at the basic infrastructure to find the fault lines. Take a look at this:

public void Log(string pSource, string pMessage, EventLogEntryType pEntryType) {
    try {
        if (!EventLog.SourceExists(pSource)) {
            EventLog.CreateEventSource(pSource, "Application");
        }

        EventLog.WriteEntry(pSource, pMessage, pEntryType);
    }
    catch (Exception _ex) {
        Log("", _ex.ToString(), EventLogEntryType.Error);
    }
}
This is… impressive.

Comments

m4bwav

awesome, I almost missed the recursion

Joel

That is impressive. I think that will lead to a StackOverflowException, right?

According to the MSDN docs, calling EventLog.CreateEventSource or EventLog.WriteEntry with the source parameter set to an empty string ("") will generate an ArgumentException.

Therefore, if during the first run through the function it throws an exception, the catch block will try and call into the Log function again (recursively) using a blank string, which will generate another exception, until a stack overflow occurs.

That's my guess. :)

addy santo

down that path insanity lies :)

Dave

And than they say programmers don't have humor ;-)

Mauríco

This might be the most brilliant try...catch block I have ever seen.

Chris Smith

Genius. Sheer genius.

Nair

I guess it is a joke right?

Nick

While leads me to a question: What is the best way to log that your logger failed?

Jose

Maybe works fine for EventLogEntryType.Error.

is a joke

Stefan

Heh I have seen this many times before, but on something like an error page, it would inherit the base page class which in on load would fail to connect to the DB and redirect to error page.

Endless redirect to error error error error, all the while sending error emails. That day I got 200,000 error emails from the site, fun fun fun.

configurator

Reminds me of something similar that we have where I used to work:

void Save() {

Recalculate();

SaveToDatabase();

}

void Recalculate() {

try {

    // some stuff

} catch {

    Save();

}

}

It's still there. "The "some stuff" shouldn't fail so why fix that part"

Of course, every once in a while someone makes a change and "some stuff" does fail, resulting in nasty bugs.

Mr Berre

Ugh, reminds me of a bit of code I discovered recently in the web ui for a new project I was working on. All aspx pages inherit from a base class, which contains a check whether a user is allowed to even see anything in the UI. If a user wasn't allowed to, he was redirected to Error.aspx, which of course inherited from the base class and which of course triggered the check...

The UI code was based on another project's code (no need to re-invent the wheel) that has been in use for 6+ months. I'm still amazed that they've never had an issue with that, since usually the business people are very good at pointing people to apps they don't have access to.

miro

sfa

Tool

If at first you don't succeed...

Paula

Brillant!

ana
ana

I have seen this many times before, but on something like an error page,

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. Paying the rent online - 3 days from now

There are posts all the way to Aug 03, 2015

RECENT SERIES

  1. Production postmortem (5):
    29 Jul 2015 - The evil licensing code
  2. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
  3. API Design (7):
    20 Jul 2015 - We’ll let the users sort it out
  4. What is new in RavenDB 3.5 (3):
    15 Jul 2015 - Exploring data in the dark
  5. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats