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 712291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:49:55+00:00 2026-05-14T04:49:55+00:00

I am looking into C# logging and I do not want my log messages

  • 0

I am looking into C# logging and I do not want my log messages to spend any time processing if the message is below the logging threshold. The best I can see log4net does is a threshold check AFTER evaluating the log parameters.

Example:

_logger.Debug( "My complicated log message " + thisFunctionTakesALongTime() + " will take a long time" )

Even if the threshold is above Debug, thisFunctionTakesALongTime will still be evaluated.

In log4net you are supposed to use _logger.isDebugEnabled so you end up with

if( _logger.isDebugEnabled )
    _logger.Debug( "Much faster" )

I want to know if there is a better solution for .net logging that does not involve a check each time I want to log.

In C++ I am allowed to do

LOG_DEBUG( "My complicated log message " + thisFunctionTakesALongTime() + " will take no time" )

since my LOG_DEBUG macro does the log level check itself. This frees me to have a 1 line log message throughout my app which I greatly prefer. Anyone know of a way to replicate this behavior in C#?

  • 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-05-14T04:49:56+00:00Added an answer on May 14, 2026 at 4:49 am

    If you can target .NET 3.5 (C# 3.0) you can use extension methods to wrap the if statements.

    so you can do the equivalent “macro”:

    logger.Log_Debug("Much faster");
    
    logger.Log_Debug(() => { "My complicated log message " + thisFunctionTakesALongTime() + " will take no time" });
    

    by wrapping the check in this method:

    public class Log4NetExtensionMethods {
        // simple string wrapper
        public void Log_Debug(this log4net.ILog logger, string logMessage) {
            if(logger.isDebugEnabled) {
                 logger.Debug(logMessage);
            }
        }
    
        // this takes a delegate so you can delay execution
        // of a function call until you've determined it's necessary
        public void Log_Debug(this log4net.ILog logger, Func<string> logMessageDelegate) {
            if(logger.isDebugEnabled) {
                 logger.Debug(logMessageDelegate());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking into using a javascript logging framework in my app. I quite like
By looking into the Open JPA website i've found that i can log the
I'm looking into the SplObserver pattern as a way of solving the logging problem
I am looking into syslog. I understand that it is a centralized logging facility
I'm looking for tips on the best way to reconfigure the Log4Net logging level
I'm trying to use console.log to put some logging into the javascript side of
I have been looking into benchmarking lately, I have always been interested in logging
Looking into selector performance between $('#ID1, #ID2, #ID3') vs $('1X CLASS'). Which is faster?
Currently looking into learn new technology and silverlight is on the potential list. However,
Am looking into developing an iPhone native app using Titanium Developer Since this is

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.