What is log4net?

what is log4net and what is it used for can it be a substitution for debugging, in what way we can utilize them? can somebody tell me about this?

Check out their website.

log4net is a .NET port of the very popular, and highly rated log4j (Java-based) logging engine. It’s not really a substitute for debugging, although it can be highly configured and can serve the purpose of debugging. It’s really a tracker for all the things that happen whilst your application is executing.

You can use it to capture event details for users, etc, or to provide audit trails, so on and so forth. It writes logs to a file (wherever you specify), although I’m not sure what sort of facilities it provides out of the box for viewing logs. I think it maybe comes down to reading formatted files - not quite sure.

But, essentially, it’s a tool to provide extensive logging facilities for .NET based applications. Check out http://logging.apache.org/log4net for all the official info.

is log4net is only solution for this? are there any alternatives available?

why don’t you create a method that writes to a text file, and each time you want to log something, call your method with what ever text you wish plus any results etc.


private void WriteLog(string Message, bool error)
	TextWriter tw = new StreamWriter("C:\\log.txt");
	tw.WriteLine("Error: " + error);

WriteLog("This is a test", false);
WriteLog("This is another test", true);


This is a test
Error: false
21/12/2005 17:07:00
This is another test
Error: true
21/12/2005 17:08:00


That works great for trivial applications. But when you are dealing with more heavyweight stuff, you will want things that logging frameworks (such as Log4Net or the Microsoft Enterprise Library) can provide.

The main benefit these frameworks provide is configuration-driven logging actions. You write your applications to log events in certain categories and priorities then let the configuration files decide exactly what happens after deployment.

Plus the advantage is that if the framework is good (which is the case with log4net or MS ent library) then you don’t have to care about many unexpected situations. E.g. in Gavs code, if you dont have access to the log file the code would raise an exception and block your application. This is not the case with log4net which will never break you program flow.

And as it was said logging is more then dumping your data into a plaintext file. You maybe want to send errors to your e-mail, write warnings and errors to Event Log or send a SNMP trap message or alter some WMI Instrumentation object. Or maybe do some custom thing such as paging your admin.

if program flow never breaks means, that the application would run to end even if some exceptions are raised, is that?

Log4net is an excellent logging framework, it is feature rich it is configuration driven, and it support output of log rows to a number of targets (trough it’s appenders) like TextFiles, “Rolling textfiles”, Database, Mail (smtp), Remote, Console and others, you can even write your own appender an plug into the framework. The use of appenders, the layout of the logging and the level which you need to log is easily changed in the configuration file. This gives you the possibility to turn on an off logging in a production environment, without having to change your application.

Using log4net you have access to excellent support through the log4net mailing list (http://logging.apache.org/log4net/support.html), a copy of the mailing list is available on several sites for example:

Microsoft EIF is an alternative; several people have compared those two, most of them are in favor of Log4net, check out the links below:


Unless you are writing a really trivial application I wouldn’t even consider using any home made solutions.

From my perspective logging is not an alternative to debugging, but it can assist in debugging. From my point of view logging is something you put into an application to see how the application is doing in the production environment, something which is essential especially in web applications.

I realized that I am not totally objective since I sell a product based on Log4Net :slight_smile:

The above is generally accurate.

I would note that the Enterprise Library (not really certain what EIF is, I suspect it is a wierd acronym for it) does alot more than logging whereas log4net is a pure-bred logging library. Not that there is anything wrong with this, but I would not call it an apples to apples comparison. There is no database abstraction layer, exception management library nor configuration management library included in log4net. With Entlib 2.0 you can also get dependecy injection thrown in, as well as not require one to register the library with WMI . . .

[FONT=Verdana]Yes EIF is a weird acronym - the marketing guys in Microsoft must be really busy :slight_smile:

EIF = Microsoft Enterprise Instrumentation Framework.

Regarding Log4Net vs EIF, Tom Hollander’s blog entry gives some Microsoft perspective (He is one of the guys behind EIF)


EIF is a bit different from the Enterprise Library’s Logging Application Block, which does not necissarily write to the event log/WMI insofar as application level logging is concerned.* A much better, apples to apples style comparision would be the Logging Application Block vs log4net.

*Unfortuantely version 1.x writes to the event log/WMI insofar as everything is concerned, which can be a problem in some scenarios where one does not have administrative access to the box one is deploying to. The point of that post is about how to remove the EIF calls which are strewn about the block. Version 2.0 is vastly superior as the it handles using the insturmentation framework via configuration and dependency injection in a totally sweet manner.