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?
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:
Then you have different implementations:
You could then use a static class as the main entry point:
Now you need to wire this up in your startup code. Here you can use a container or use conditional compilation:
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.