Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 470507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:53:30+00:00 2026-05-12T23:53:30+00:00

This is more of a theoretical question. Should logging reside within a class who’s

  • 0

This is more of a theoretical question. Should logging reside within a class who’s primary purpose is not logging?

Here is a simple interface for anything that will preform a calculation on a number.

public interface ICalculation { 
    public int calculate(int number); 
}

Here is an implementation of the ICalculation interface that performs a calculation and does some logging. I believe this is a very pragmatic approach. Aside from the constructor accepting something that we wouldn’t normally expect to see in a calculation’s domain, the inline logging is arguably non intrusive.

public class ReallyIntenseCalculation : ICalculation {
    private readonly ILogger log;

    public ReallyIntenseCalculation() : this(new DefaultLogger()) {
    }

    public ReallyIntenseCalculation(ILogger log) {
        this.log = log;
        log.Debug("Instantiated a ReallyIntenseCalculation.");
    }

    public int calculate(int number) {
        log.Debug("Some debug logging.")
        var answer = DoTheDirtyWork(number);
        log.Info(number + " resulted in " + answer);
        return answer;
    }

    private int DoTheDirtyWork(int number) {
        // crazy math happens here
        log.Debug("A little bit of granular logging sprinkled in here.");
    }
}

After removing all the logging code from ReallyIntenseCalculation, the code now has what appears to be a clear single responsibility.

public class ReallyIntenseCalculation : ICalculation {

    public int calculate(int number) {
        return DoTheDirtyWork(number);
    }

    private int DoTheDirtyWork(int number) {
        // crazy math happens here
    }
}

Ok, so we have removed ReallyIntenseCalculation’s ability to log its internals. How can we find a way to externalize that functionality. Enter the decorator pattern.

By creating a class that decorates an ICalculation we can add logging back into the mix, but doing so will compromise some of the more granular logging that was taking place within ReallyIntenseCalculation’s private methods.

public class CalculationLoggingDecorator : ICalculation {
    private readonly ICalculation calculation;
    private readonly ILogger log;

    public CalculationLoggingDecorator(ICalculation calculation, ILogger log) {
        this.calculation = calculation;
        this.log = log;
        log.Debug("Instantiated a CalculationLoggingDecorator using " + calculation.ToString());
    }

    public int calculate(int number) {
        log.Debug("Some debug logging.")
        var answer = calculation.calculate(number);
        log.Info(number + " resulted in " + answer);
    }
}

What are some of the other possible pros and cons of having a logging decorator?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:53:31+00:00Added an answer on May 12, 2026 at 11:53 pm

    I think it’s debatable. A case can be made that logging is part of that single responsibility.

    That said, I think you’re thinking in terms of cross-cutting concerns, which is something aspect-oriented programming deals with. In fact, logging code is the canonical example for AOP. You may want to consider an AOP framework like aspect#.

    The advantages of doing something like this are of course decomposability, reuse, and separation of concerns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a theoretical question, so expect that many details here are not computable
Well I assume this is more theoretical question for those who familar with hft.
This is more of a theoretical question than an actual problem I have. If
This is more of a design question. I have a template class, and I
this is more a theoretical question i asked myself. I remembered that BinarySearch of
This is a more theoretical question about macros (I think). I know macros take
This question is more theoretical. Is it save to be passing email address in
This is more of a theoretical question rather than language specific, but I'm mostly
this is more theoretical question. I have written simple chat with my friend. We
This is more of a theoretical question than a specific one. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.