How To Not Log

Today’s Tip has a weird title. Let’s get started and find out how not to log.

Say, we have the following validation method with logging statements sprinkled throughout:

  private void Validate(CustomerRegistration registration)
  {
     if (registration == null)
        throw new MissingCustomerRegistration();
     registration.Validate();
     Logger.LogInfo("The registration is valid.");
     var customer = Repository.GetCustomer(registration.EmailAddress);
     if (customer != null)
        throw new CustomerAlreadyExists(registration.EmailAddress);
     Logger.LogInfo($"'{registration.EmailAddress}' is a new customer email address.");
  }

The Logger reference is of type ILogger, a logging interface.

We want to remove all logging from our system temporarily, including Validate(). What are our options? How can we implement not logging? Here are a few options:

 

Comment Out Logging Calls

We could comment out all calls to Logger in our system. For example, Validate() would change to

  private void Validate(CustomerRegistration registration)
  {
     if (registration == null)
        throw new MissingCustomerRegistration();
     registration.Validate();
     // Logger.LogInfo("The registration is valid.");
     var customer = Repository.GetCustomer(registration.EmailAddress);
     if (customer != null)
        throw new CustomerAlreadyExists(registration.EmailAddress);
     // Logger.LogInfo($"'{registration.EmailAddress}' is a new customer email address.");
  }

It’s not a great solution. If we have 200 logging calls, then we’ll have to make 200 changes in our code! This process is bound to be error-prone and take up much valuable developer time. Also, when we want to restore logging, we’ll need to undo all those commented out Logger calls. This solution violates the SOLID Open-Closed Principle (OCP). The OCP declares that we ought to design our software so we may extend it with new code rather than having to modify existing code.

 

Remove Logging Calls

Alternatively, we could remove all calls to Logger methods and, when needed again, restore the logging code from source control. That seems like a reasonable approach. However, it has drawbacks, too: Along with the logging calls, we could accidentally reintroduce old, obsolete source code. The result might be bugs or a broken system.

 

Comment Out Inside Logger

A better option would be to comment out (or remove) the code inside an ILogger implementer. Assuming our used ILogger implementation is ConsoleLogger, we can comment out the implementation code like so:

  public class ConsoleLogger : ILogger
  {
     public void LogInfo(string message)
     {
        // Console.WriteLine(message);
     }
     // ...
  }

It’s not perfect, but it’s pretty good and would work. Unfortunately, the class name would be misleading since it’s no longer logging to the console. And once again, we are running afoul of the Open-Closed Principle (OCP)—we modified existing code and would prefer to write new code.

Is there a way we can not log by only writing new code?

I’ll reveal the answer next time.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply