Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net versus TraceSource [closed]

In this thread many people have indicated that they use log4net. I am a fan of TraceSources and would like to know why log4net is used.

Here is why I like trace sources:

  • Pluggable listeners - XML, TextFile, Console, EventLog, roll your own
  • Customisable trace switches (error, warning, info, verbose, start, end, custom)
  • Customisable configuration
  • The Logging Application Block is just a big set of TraceListeners
  • Correlation of activities/scopes (e.g., associate all logs within an ASP.NET request with a given customer
  • The Service Trace Viewer allows you to visualize events against these activities individually
  • All of it is configurable in app.config/web.config.

Since the .NET framework internally uses TraceSources, it also gives me a consistent way of configuring tracing - with log4net, I have to configure log4net as well as TraceSources.

What does log4net give me that TraceSources don't (or that couldn't be done by writing a couple of custom TraceListeners)?

like image 755
Paul Stovell Avatar asked Feb 23 '09 03:02

Paul Stovell


People also ask

Why do we need log4net?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

What is log4net in API?

Log4net is an open source project based on the work of many authors. It allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files. Almost every large application includes its own logging or tracing API.

What is log4net in Java?

What is log4net. Log4net is an open source library that allows . NET applications to log output to a variety of sources (e.g., The console, SMTP or files). Log4net is a port of the popular log4J library used in Java.


1 Answers

In the very early days (.NET 1.0) tracing in the .NET Framework was pretty limited.

For example TraceSource partitioning didn't come until .NET 2.0 and you only had four levels (Error, Warning, Information, Verbose), although you could use half a dozen boolean switches for partitioning if you wanted.

log4j is popular in Java and so got a lot of support for a .NET port, and once it became popular it kind of stayed that way, even though people don't even use it properly (e.g. wrapping it in a singleton logger and losing it's main feature).

Still, I think that log4net and other frameworks (e.g. NLog, Common.Logging, and even EntLib) went the wrong way by implementing their own logging system from the ground up, i.e. changing even the way you write log statements in the first place.

I would have much preferred to see effort, especially since .NET 2.0, put into extending the solid basis of what is already in .NET. For a project that does extend what is already there, have a look at the Essential Diagnostics project on CodePlex (http://essentialdiagnostics.codeplex.com/).

Some strengths of log4net:

  • It is similar to log4j, if you run a mixed environment and want consistent logging.

  • Automatic logger hierarchy that inherits settings is quite neat, compared to how many trace sources you implement and have to configure each. (although probably overkill in some cases).

  • log4net already has around 28 appenders (equivalent to trace listeners), whereas System.Diagnostics only has 10 (but see the Essential.Diagnostics project for more), so if you really think you may need the RemoteSyslogAppender, NetSendAppender, AnsiColorTerminalAppender or TelnetAppender, then you are in luck.

Drawbacks (compared to System.Diagnostics):

  • You need to use different logging syntax, so if you are already using source.TraceEvent(), you need to go through and replace everything.

  • This also extends to different syntax for correlation, so you need to change from CorrelationManager to log4net contexts.

  • Doesn't easily integrate with Framework tracing (e.g. WCF).

  • Poor support for Event ID's (need to use a separate extension project IEventLog).

  • Doesn't yet support Event Tracing for Windows (Vista), or the Service Trace Viewer XML format.

like image 147
Sly Gryphon Avatar answered Oct 21 '22 17:10

Sly Gryphon