What is the difference between log4net and ELMAH?

38,669

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

ELMAH:

  • Web application

Log4net:

  • Web application

  • Windows application

Additional references: ref_1 ref_2

Share:
38,669

Related videos on Youtube

IAdapter
Author by

IAdapter

Updated on July 08, 2022

Comments

  • IAdapter
    IAdapter almost 2 years

    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#?

    • coopejoh
      coopejoh about 13 years
    • Chaitanya Gadkari
      Chaitanya Gadkari over 6 years
      Now that .net core is in picture, does the scenario change? (elmah does not support .net core as of Jan 2018).
  • IAdapter
    IAdapter about 13 years
    What are those advantages? I will do ASP.NET soon, so I would like to know your educated opinion. I'm still begginer in .NET, so its harder for me to interpret what they are saying on their sites.
  • Adeel
    Adeel about 13 years
    You can view reports even without writing any code. See elmah features here code.google.com/p/elmah
  • Dustin Davis
    Dustin Davis about 13 years
    Elmah is very limited and the only "avantage" is the reports but it is limited to web apps which have access to HttpContext. You can have other loggers log data to many other sources that can be reported on.
  • Ed DeGagne
    Ed DeGagne about 12 years
    One correction, Elmah does have an Api that the developer can interact with. For instance, signaling is achieved through the Elmah Api.
  • DarthVader
    DarthVader almost 12 years
    log4net gives you more control.
  • DarthVader
    DarthVader almost 12 years
    ELMAH captures a wealth of useful information out of the box, if you configure properly. for the same amount of time, you can do the same with log4net with more control.
  • JasonD
    JasonD almost 12 years
    DarthVader - do you have a sample log4net configuration that does the same thing?
  • desperateCoder
    desperateCoder over 11 years
    ELMAH captures a wealth of useful information out of the box with NO configuration. With extra configuration it will handle both unhandled exceptions and handled exceptions.
  • Sturla
    Sturla about 10 years
    You could get the best of both worlds by using this ELMAH Appender for log4net. Just log what you like (debug,info,warn,error) with log4net but store it in ELMAH nuget.org/packages/elmahappender_log4net_1.2.10
  • Maxime Rouiller
    Maxime Rouiller almost 10 years
    Another correction? ELMAH is a .NET HttpModule/HttpHandler. It is NOT an IIS plugin. ARR is a plugin. Not ELMAH.
  • Peter Mortensen
    Peter Mortensen over 6 years
    What is your response to the comments?