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?
Code pollution refers to a pattern that highlights the complexity of a code in specific unit testing. Code pollution takes place when developers include more code in their database to make unit testing work. The simple solution to avoid code pollution is to not introduce additional code production.
Logging is essential to understand the behaviour of the application and to debug unexpected issues or for simply tracking events. In the production environment, we can't debug issues without proper log files as they become the only source of information to debug some intermittent or unexpected errors.
Logs are also useful to detect common mistakes users make, as well as for security purposes. Writing good logs about a user's activity can alert us about malicious activity. It is important that logs can provide accurate context about what the user was doing when a specific error happened.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With