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

  • SEARCH
  • Home
  • 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 8103217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:28:43+00:00 2026-06-05T23:28:43+00:00

Couldn’t remember better title, feel free to change it to something better :) My

  • 0

Couldn’t remember better title, feel free to change it to something better 🙂

My application contains a lot of logging statements, something like this:

var logger = new Logger(/*..get flag from settings if the logger should be active..*/);
// ....
logger.LogActivity(..serialize an object..);
//...
logger.LogActivity(..get another object's expensive overriden ToString method..);
//...
logger.LogActivity(..log something new..);

Logger class:

public class Logger
{
  private readonly bool _isActive;

  public Logger(bool isActive)
  {
    _isActive = isActive;
  }

  public void LogActivity(string activity)
  {
    if (_isActive)
    {
      // Save activity to Database.
    }
  }
}

When I disable logger in settings (so the _isActive field in Logger class is false), then nothing is saved to database. But all the expressions in the Logger.LogActivity methods are still evaluated (for instance ..serialize object.. in previous example) and this slows down my application.
I could use log statements like this:

var logger = new Logger(/*..get flag from settings if the logger should be active..*/);
// ....
if (loggerIsActive) logger.LogActivity(..serialize an object..);
//...
if (loggerIsActive) logger.LogActivity(..get another object's expensive overriden ToString method..);
//...
if (loggerIsActive) logger.LogActivity(..log something new..);

But it would be much better to change only LogActivity method… Is it possible to somehow change only LogActivity method so that in case, when the logger is disabled, the expressions in LogActivity call aren’t evaluated? Well – I would be surprised if this is possible in C#, but is there any other way/pattern to do this?

  • 1 1 Answer
  • 0 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-06-05T23:28:45+00:00Added an answer on June 5, 2026 at 11:28 pm

    You could add an overload that takes a Func<string> that would generate the string to be logged.

    public void LogActivity(Func<string> activity)
    {
        if (_isActive)
        {
            string log = activity();
            // save 'log' to database
        }
    }
    

    Then use it like this:

    logger.LogActivity(() => expensiveObject.ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(couldn't think of a better title, feel free to edit it to a title
Couldn't seem to think of a good title, my apologies. Feel free to change
Sorry couldn't think of a better title. :) Here goes, I have been pulling
Hi couldn't find it for C#, I am trying something like that for (int
couldn't think of a better title. I just want someone to let me know
(couldn't think of a better title, sorry) So I've got my layout page, on
Couldn't find better title but i need a Regex to extract link from sample
I couldn't find a better title to describe my problem. I want to achieve
I couldn't think of a way to word the title, so sorry for that,
I couldn't find a suitable title for this. I'm going to express my query

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.