47

I'm looking into how to implement logging in my C# app - its a DLL class library. What logging frameworks are most widely used - what would give users of my DLL the most flexibility and configurability? Is there a C# equivalent of log4j?

Nosrama
  • 14,530
  • 13
  • 49
  • 58
  • 1
    I would give ReflectInsight a try http://http://insightextensions.codeplex.com/ – code5 Aug 22 '13 at 13:38
  • There is a list of logging frameworks here: https://github.com/quozd/awesome-dotnet/blob/master/README.md#logging – James Ko Feb 24 '18 at 04:48

7 Answers7

50

2009 Answer: Equivalent of log4j for .NET platform is log4net and I am guessing it's widely used.


2019 Answer: Here are a variety of alternatives from https://github.com/quozd/awesome-dotnet/blob/master/README.md#logging:

  • Essential Diagnostics - Extends the inbuilt features of System.Diagnostics namespace to provide flexible logging
  • NLog - NLog - Advanced .NET and Silverlight logging
  • Logazmic - Open source NLog viewer for Windows
  • ELMAH - Official ELMAH site
  • Elmah MVC - Elmah for MVC
  • Logary - Logary is a high performance, multi-target logging, metric, tracing and health-check library for Mono and .NET. .NET's answer to DropWizard. Supports many targets, built for micro-services.
  • Log4Net - The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets
  • com.csutil.Log - A lightweight zero config Log wrapper that can be combined with other logging libraries like Serilog for more complex usecases.
  • Serilog - A no-nonsense logging library for the NoSQL era. Combines the best of traditional and structured diagnostic logging in an easy-to-use package.
  • StackExchange.Exceptional - Error handler used for the Stack Exchange network
  • Semantic Logging Application Block (SLAB) - Extends the inbuilt features of System.Diagnostics.Tracing namespace (EventSource class) to log to several sinks including Azure Tables, Databases, files (JSON, XML, text). Supports in-process and out-of-process logging through ETW, and Rx for real-time filtering/aggregating of events.
  • Foundatio - A fluent logging API that can be used to log messages throughout your application.
  • Exceptionless - Exceptionless .NET Client
  • Loupe - Centralized .NET logging and monitoring. [Proprietary] [Free Tier]
  • elmah.io - Cloud logging for .NET web applications using ELMAH. Find bugs before you go live. Powerful search, API, integration with Slack, GitHub, Visual Studio and more. [Free for OSS] [$]
  • BugSnag - Logs errors. Includes useful diagnostic info like stack trace, session, release, etc. Has a free tier. [Free for OSS][$]
Cagdas Altinkaya
  • 1,710
  • 2
  • 20
  • 32
  • 2
    I use log4net in nearly all my .NET applications. However, my classes do not hold a reference to log4net directly, I hide this infrastructure concern behind an interface and use dependency injection. – JohnRudolfLewis Aug 11 '09 at 13:02
  • We're also using log4net. It is just great. Incredibly flexible, incredibly fast. – Stefan Steinegger Aug 11 '09 at 13:07
  • use log4net and never look back... Doesnt SO use log4net? – Allen Rice Aug 11 '09 at 13:28
  • log4net ideology starts to be a bit old. Check e.g. Logary for fresh ideas: https://github.com/logary/logary – Tuomas Hietanen Aug 19 '16 at 14:13
  • According to this https://www.loggly.com/blog/benchmarking-5-popular-net-logging-libraries/ log4net is very slow. – QtRoS Aug 27 '18 at 15:07
  • I also wrote open source log viewer that support many log frameworks:https://github.com/Analogy-LogViewer/Analogy.LogViewer – Lior Jul 25 '20 at 19:39
6

Have used NLog successfully in numerous projects.

ozczecho
  • 8,649
  • 8
  • 36
  • 42
3

We use our own logging classes, implemented by calling log4net. This allows us to take advantage of this flexible and widely-used framework while avoiding thousands of direct references to it in the source code.

azheglov
  • 5,475
  • 1
  • 22
  • 29
3

log4net is almost certainly the most common.

But I use Common.Logging - http://netcommon.sourceforge.net/ as it gives me flexibility

There are a variety of logging implementations for .NET currently in use, log4net, Enterprise Library Logging, NLog, to name the most popular. The downside of having differerent implementation is that they do not share a common interface and therefore impose a particular logging implementation on the users of your library.

Common.Logging library introduces a simple abstraction to allow you to select a specific logging implementation at runtime. Thus you can defer the decision what particular logging library to use until deployment. Adapters are used for plugging a particular logging system into Common.Logging.

gef
  • 7,025
  • 4
  • 41
  • 48
  • What is the benefit of deferring the decision? – BenKoshy Sep 10 '17 at 22:38
  • 1
    Wow, this is over 8 years old. As Common.Logging said (and still does), you can defer your decision till deployment. For example; Maybe you want to whitelabel your system? And devops for the company who want to host it prefer Enterprise Library Logging, whilst another devops team prefer log4net. – gef Sep 10 '17 at 23:07
  • I'm interested in using Common.Logging (thanks for the tip, BTW), but am weary of the performance overhead incurred by the abstraction layer / runtime resolution. Do you perhaps know of any benchmarks pertaining to this? – CShark Oct 23 '17 at 12:34
  • 1
    Afraid not @DaffyPunk, it's been over six years since I used .NET. But I'd imagine it's negligible and nothing to worry about?: http://wiki.c2.com/?PrematureOptimization ;) – gef Oct 31 '17 at 15:26
1

I am using NLog from years with success and it is very well done project.

twk
  • 3,122
  • 2
  • 26
  • 36
0

People have been using Enterprise Library extensively. But it may be true that developers are switching to other products these days.

I'd check it out and see if it has the necessary functionality you need and not too much bloat.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0

Enterprise Library. It's robust and comes straight from Microsoft with all their best practices included. We use it in all our projects. It's very flexible and there is a UI tool that you can use in case you don't want to mess around with managing logging from the config file.

Punit Vora
  • 5,052
  • 4
  • 35
  • 44
  • 1
    Downvoting because Enterprise Library is not robust. It is slow, adds ridiculous overhead, and the API is not great, either. – John Zabroski Aug 22 '15 at 13:50
  • @John regardless of your opinion about enterprise library, it still is probably the most widely used (probably because it directly comes from Microsoft) and provides the flexibility and configurability needed as per the question. I believe it is a perfectly valid answer to the question. – Punit Vora Aug 28 '15 at 18:41
  • What makes it widely used? EntLib.Logging has 280K downloads (https://www.nuget.org/packages/EnterpriseLibrary.Logging/ ) on NuGet compared to 2.6M for log4net ( https://www.nuget.org/packages/log4net/ ). In fact NLog has 1.5M and Serilog has 130K. My main objection is you called it Robust. All the teams I worked with regretted using it. By the way, "it is slow and adds ridiculous overhead" is NOT an opinion. – John Zabroski Aug 28 '15 at 20:10
  • @John that a wrong benchmark because enterprise library is available and was downloaded as an .msi since way before nuget arrived. We have used it pretty nicely without the regret that your teams seem to have experienced. And yes, I have also used log4net before. To each his own I guess... – Punit Vora Aug 31 '15 at 22:02
  • 2
    I am not sure what you are trying to argue now. We were talking robustness and popularity. Benchmarking shows EL is 3 orders of magnitude(!!!) slower than all others. http://essentialdiagnostics.codeplex.com/wikipage?title=Comparison You are probably incurring overheads in your app without realizing it. That being said, you can write an extension method to the EL LogWriter to solve this problem, but the point is EL sucks out of the box. "To each his own" is not a valid engineering argument! We are engineers, not politicians! – John Zabroski Sep 01 '15 at 14:42
  • 3
    @JohnZabroski From wikipedia: _In computer science, robustness is the ability of a computer system to cope with errors during execution. Robustness can also be defined as the ability of an algorithm to continue operating despite abnormalities in input, calculations, etc._ You downvote because of the word _robust_ but all your arguments are based on performance hit. EL Logger, by default, is made to swallow exceptions so it's definitely robust as it won't mess with your program flow. – imlokesh Dec 25 '15 at 17:19
  • 1
    @imlokesh - all logging frameworks can interfere with program flow, by changing the timing of your threads and when context switches occur in your program flow. I wouldn't define robust program flow as simply swallowing exceptions, either, as the best place to handle that 'robustness' is in the application code. A swallowed exception does not guarantee continued operation of a program, sadly (oh, if it were that easy!). – John Zabroski Dec 28 '15 at 14:57
  • 2
    @JohnZabroski I'm just saying your downvote wasn't justified. – imlokesh Dec 28 '15 at 18:24