Some people are using ELMAH instead of log4net. What makes it better?
I found out about ELMAH in an answer to Stack Overflow question How do I do logging in C#?
Summary. ELMAH provides a simple, yet powerful mechanism for logging errors in an ASP.NET web application. Like Microsoft's health monitoring system, ELMAH can log errors to a database and can send the error details to a developer via email.
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.
There is some small difference between NLog and log4net. NLog is easier to configure, and supports a much cleaner code-based configuration than log4net. I think the defaults in NLog are also more sensible than in log4net.
log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.
Log4Net is a general purpose logging framework with an API intended to be used within your application (web, console, dll, etc.).
logger.Debug("Some low level debug message...");
logger.Info("Some basic info");
logger.Warn("Some business logic problem, but not critical");
logger.Error("An unexpected error");
ELMAH is an unobtrusive IIS plugin specifically for logging exceptions in a web application. You won't see a reference to ELMAH within your application, it doesn't have an API that you interact with. It uses the module and handler IIS extension points to bolt in the behavior. In addition it has a web front-end to view the errors that have occurred in your web application. Log4Net doesn't have a front-end, just a variety of log sinks (Appenders) that can send your log messages to things like log files, a syslog server, a database, etc.
ELMAH serves a purpose of tracking errors and exceptions for your web applications and allows you to easily log or view those exceptions via many different mechanisms (SQL, RSS, Twitter, files, email, etc.). If you have no built-in exception handling ELMAH will most likely get you what you are looking for in terms of exception handling in a web application environment.
Log4net can be used for exception logging as well, however you might need to roll your own handlers to plug into your web application. Log4net will shine over ELMAH if you need to do other types of information logging as log4net is a general purpose logging framework. Log4net can also be used in almost any .NET application.
The key difference is that ELMAH logs unhandled application exceptions; log4net logs whatever you tell it to log. You can configure log4net to log unhandled exceptions, but ELMAH captures a wealth of useful information out of the box.
ELMAH works by creating an HTTP module that hooks into error events and then does logging. This can easily be thwarted and broken by bad design. Log4Net might require additional work upfront but if you use an AOP framework and apply it across your class or even the assembly, you can achieve far better exception logging with very little code and effort.
ELMAH only works if it has access to HttpContext which can be hard to achieve in WCF services. So you would end up using some other logger in WCF anyway. Why not just use one solution that is more universal.
ELMAH:
Log4net:
Web application
Windows application
Additional references: ref_1 ref_2
ELMAH is specially used for web applications while log4net is used on both web and Windows applications. ELMAH has many more advantages than log4net in a web application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With