Darius Kucinskas
Darius Kucinskas

Reputation: 10671

introduce logging without source code pollution

this question is nagging in my head for some time now... For logging to be useful it should be every there in the code, but then it makes code hard to read. Like the following code:

public IDictionary<decimal, Status> GetStatus(decimal[] keys)
{
    _logger.Debug("ENTERED GetStatus");

    IDictionary<decimal, Status> statuses = new Dictionary<decimal, Status>();
    string inClause = null;

    inClause = FormatInClause(keys, inClause);
    _logger.DebugFormat(" inClause: '{0}' ", inClause);
    if (string.IsNullOrEmpty(inClause))
    {
        _logger.Error("Key collection is null or empty.");
        throw new Exception("Key collection is null or empty.");
    }

    if (!IsOpen)
        Connection.Open();

    using (IDbCommand cmd = Connection.CreateCommand())
    {
        cmd.CommandText = " select id, date, status " +
            " from ORDERS where id in ( " + inClause + " ) ";

        inClause = null;

        using (IDataReader reader = cmd.ExecuteReader())
        {
            int i = 0;
            while (reader.Read())
            {
                object[] values = new object[reader.FieldCount];

                reader.GetValues(values);
                DebugHelper.LogValues(_logger, " reader.Read() #" + i + " reader.GetValues(values): ", values);

                statuses[(decimal)values[0]] = new Status(
                    (decimal)values[0],
                    ValueOrDefult<string>(values[1]),
                    ValueOrDefult<string>(values[2]),
                    (decimal)values[3],
                    ValueOrDefult<DateTime>(values[4]));
                _logger.DebugFormat(" reader.Read() #{0} created new Status() ", i);

                values = null;
                i++;
            }
        }
    }

    _logger.Debug("EXITED GetStatus");
    return statuses;
}

Is there some strategy for logging not to reduce readability of source code?

Upvotes: 17

Views: 2653

Answers (5)

jgauffin
jgauffin

Reputation: 101150

imho your logging is cluttered because your code is so too. You should read up on SOLID principles.

For instance, move the reader code to a separate method.

Upvotes: 3

MaLio
MaLio

Reputation: 2530

Look at an aspect weaver such as PostSharp

Upvotes: 0

CtrlDot
CtrlDot

Reputation: 2513

You can follow a couple rules.

1) Only log errors where you are actually "dealing" with them.

2) Use AOP to wrap your methods so you don't have to have debuging statements on enter and exit of all methods. You can also have the AOP calls log the in-coming and out-going parameters/responses of methods.

Upvotes: 1

bottlenecked
bottlenecked

Reputation: 2157

Aspect oriented programming is supposed to help with cross-cutting concerns like logging, eg. postsharp but you cannot really have very fine grained control over what is logged unless you resort to more traditional methods

Upvotes: 8

CoolStraw
CoolStraw

Reputation: 5390

Your source code looks fine to me... Actually it looks best that way, because I can see the log messages and figure what's in between each 2 messages.

Even though, one thing is bothering me, it's indeed the _ in _logger.

Some logging apis tend to propose a shortened API like:

l.d("debug")
l.c("critical")
...etc

The way above or your way are both nice imho.

EDIT

If you still want to do something about it, just wrap your logging lines into #regions and collapse them.

Upvotes: -2

Related Questions