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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:52:13+00:00 2026-05-29T10:52:13+00:00

In my C# project, I use log4net for debugging. But for the Release build,

  • 0

In my C# project, I use log4net for debugging. But for the Release build, I need to remove any dependency to log4net. I’m not sure what is the right way to go about it.

Having #if DEBUG … endif through the code is very messy, and I have to manually add/remove the Reference to log4net when I compile in Debug or Release mode.

The other option I thought about it is to somehow switch the “real” lotg4net with a mock class in the Release build, but I’m not sure how to do this.

What is the best way to remove a dependency, log4net in my case, in the Release build?

  • 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-29T10:52:14+00:00Added an answer on May 29, 2026 at 10:52 am

    Along the lines of M.Babcock’s answer: you are after dependency inversion. You do not necessarily have to use a dependency injection container but you will need to abstract your logging.

    Something like this:

    public interface ILog
    {
        void Trace(string message);
        void Debug(string message);
        void Error(string message);
        // and whatever you need
    }
    

    Then you have different implementations:

    public class NullLog : ILog { ... } // does nothing --- all calls are empty
    public class Log4NetLog : ILog { ... } // initializes Log4Net and does logging
    

    You could then use a static class as the main entry point:

    public static class Log
    {
        private ILog log = new NullLogger();
    
        public static void Assign(ILog log)
        {
            this.log = log;
        }
    
        public static void Debug(string message)
        {
            log.Debug(message);
        }
    
        // ...and other implementations...
    }
    

    Now you need to wire this up in your startup code. Here you can use a container or use conditional compilation:

    #if DEBUG
        Log.Assign(new Log4NetLogger);
    #endif
    

    These are the broad strokes. I have some logging infrastructure code as part of my service bus: http://shuttle.codeplex.com/

    ILog:
    http://shuttle.codeplex.com/SourceControl/changeset/view/c49f328edd17#Shuttle.Core%2fsource%2fShuttle.Core.Infrastructure%2fLogging%2fILog.cs

    NullLog:
    http://shuttle.codeplex.com/SourceControl/changeset/view/c49f328edd17#Shuttle.Core%2fsource%2fShuttle.Core.Infrastructure%2fLogging%2fNullLog.cs

    Log4NetLog:
    http://shuttle.codeplex.com/SourceControl/changeset/view/c49f328edd17#Shuttle.Core%2fsource%2fShuttle.Core.Infrastructure.Log4Net%2fLog4NetLog.cs

    Hope that helps.

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

Sidebar

Related Questions

I use log4net in just about every code project. I was wondering what other
I decided to use log4net as a logger for a new webservice project. Everything
Most of the developers on my current project use a (to me) strange way
I create Grails project use STS IDE . And sometimes i see not correct
I use log4net all the time, but one thing I've never figured out is
Yesterday i implement Log4Net in my application. To use it i need to write
I need to one of my project use a tree structure. I found this
Is there a way to have a project use MVC1 in certain areas and
I want to use NHibernate in my project, and it comes with log4net by
When I compile our project use Delphi 2010 Trial, there has a fatal error

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.