48

I'm somewhat new to the .NET stack and I was wondering if there is an equivalent to slf4j for the .NET platform. For me, logging to a Facade and being able to swap out logging implementations as needed just makes sense. Furthermore, the wrapper APIs available in slf4j have saved me many times when I needed to use a third-party library that was coded against a single logging framework that I wasn't using.

Is there a project out there that acts as a facade between loggers like log4net, nLog and Enterprise Library? Are there wrappers that allow me to shortcut calls to those libraries and direct them to another library? Should I start out an open source project to do this myself? Is this question a duplicate because I don't know the right way to ask? Conversely, is the common way to do this using aspect orient programming?

Elijah
  • 13,368
  • 10
  • 57
  • 89
  • Good question - i have this problem also. It just took me a few minutes to implement an ILog interface and implementation for my favourite nLog. Let's create a OS project - I would join :) – twk Apr 28 '10 at 22:28
  • 1
    Been done already. See below for link to common logging for .NET. – Tom Cabanski Apr 28 '10 at 22:31
  • 3
    There is also a SLF for .NET: http://slf.codeplex.com/, based on the slf4j. – Steven Apr 28 '10 at 22:42
  • 1
    Steven, you should make that an answer to the question. – Elijah Apr 28 '10 at 22:51
  • There is [`Microsoft.Extensions.Logging`](https://github.com/aspnet/Logging) as a part of ASP.NET Core, which has a couple of interfaces which can be implemented for the established logging frameworks, for eg, there exist `Serilog.Extensions.Logging`, `NLog.Extensions.Logging` etc. I haven't tried it, so I do not know if there is any problem in using them outside ASP.NET context. If not I can see it becoming the de facto logging facade for .NET – nawfal Mar 24 '16 at 12:03

8 Answers8

20

Excuse I used this thing and I forgot it wasn't the Apache version. It's actually open-source and part of a project called common infrastructure. It is also called common logging. It works with MS Enterprise, log4net and others. It works well.

skiphoppy
  • 97,646
  • 72
  • 174
  • 218
Tom Cabanski
  • 7,828
  • 2
  • 22
  • 25
  • 3
    Yep, Common Logging is THE logging facade for .NET – Steven Apr 28 '10 at 22:40
  • @Steven: True, but it's so out-of-date, unfortunately. – abatishchev Oct 31 '11 at 08:04
  • I would even advice against the use of logging facades. Proper dependency injection eliminates the use of any logging facade. – Steven Oct 31 '11 at 11:17
  • @abatishchev: Even so, after looking through the source code it looks like quite a well designed library. There are many nice features such as FormatMessageCallback. I am going to try it out and if it works for us, I will contribute updates where needed. – aaronc Jan 12 '12 at 20:44
  • 2
    @abatishchev: 2.1.1 was released on June 9, 2012. – Lee Chee Kiam Aug 28 '12 at 03:57
  • @Steven you still need the logging API from somewhere. – Thorbjørn Ravn Andersen Oct 04 '12 at 07:53
  • @ThorbjørnRavnAndersen: Well, you need to log somewhere, but you don't (and shouldn't) have to reference it anywhere but in your [Composition Root](http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx). Also take a look at [this answer](http://stackoverflow.com/a/9915056/264697) that talks about minimizing the need for logging. – Steven Oct 04 '12 at 08:01
  • You still need an API to use in your program regardless of how you get the actual logger. In addition to that the "log less" idea is nice, but you need to be careful - minimalistic logs tend to be less useful in forensic analysis. – Thorbjørn Ravn Andersen Oct 04 '12 at 08:05
  • @ThorbjørnRavnAndersen: I don't say you shouldn't log, but what I say is that there should be just a few lines in your total code base that actually write to log. It ofcourse depends on the requirements of your aplication, but when you use [commands to execute any business logic](http://bit.ly/s7tGEH) for insance, you can just log the complete command and when doing this, you will normally have all the information you need, even for forensic analysis. Add [event sourcing](http://bit.ly/p1WJe) and you'll always have a complete picture of what happened at every point in time. – Steven Oct 04 '12 at 11:09
  • 1
    @ThorbjørnRavnAndersen: I tend to define my own `ILogger` interface and implement an adapter to the using logging framework (see [here](http://stackoverflow.com/a/5646876/264697)). This keeps my application free from any references to the logging framework. In other words: create your own API. – Steven Oct 04 '12 at 11:09
  • 5
    @Steven glad you have found a procedure that works for you. I've personally found that being able to stand on the shoulders of others is a benefit to us, so I usually rely on a logging component. slf4j is great for a common api to those. – Thorbjørn Ravn Andersen Oct 04 '12 at 11:19
  • My team gave me a reason against using a facade: You don't know if/when they might change the api. We do have projects that gets included in other projects, so if they do change something, we might end up with different versions that wouldn't be compatible. – Tigerware Feb 15 '19 at 08:24
10

Take a look at Castle Windsor Logging Facility.

Base code is here. Log4net adapter here. NLog adapter here.

Added adapter for Serilog.

Without the adapters there is suppor for ConsoleLogger, DiagnosticsLogger, StreamLogger and NullLogger.

It's pretty easy to write adapters to any other logging framework.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 1
    Castle.Services.Logging seems to have disappeared. Getting a 404 – Sam7 Sep 27 '12 at 08:02
  • 1
    @Sam7 no, just the links changed. Whenever you find something like this, please consider googling for a bit to find the new links (as I just did), then submitting a suggestion to the answer. Thanks! – Mauricio Scheffer Sep 27 '12 at 14:44
9

I'm incorporating Simple Logging Facade because it appears to support The Object Guy's Logging Framework for .Net out of the box.

quentin-starin
  • 26,121
  • 7
  • 68
  • 86
7

Update for .NET Core:

shatl
  • 911
  • 16
  • 21
  • Update: as the readme for LibLog says, "My recommend course of action for library developers is to use Microsoft.Extensions.Logging.Abstractions as it is now the defacto standard logging interface in the .NET ecosystem.". https://github.com/damianh/LibLog#liblog-- – alelom Apr 08 '23 at 07:10
6

I had the same Problem. After going through those recommended above, I discovered Ninject.Extensions.Logging

Which is great since a facade makes best sense in combination with DI anyway

It comes with proxies for NLog, NLog2 & log4net.

Here is a nice example on how to use it in combination with NLog: http://blog.tonysneed.com/2011/10/09/using-nlog-with-dependency-injection/

I understand, that this question is already a couple of years old, but due to completeness for other users, I still chose to post this 'Solution'

Sam7
  • 3,382
  • 2
  • 34
  • 57
4

Since this was written another interesting logging wrapper called Fody.Anotar has appeared. As of today, it's probably the most complete of all of them. Most of the other, but Logging Faciliy, are quite outdated, supporting only the oldest loggers.

It has adapters for NLog, Log4Net, Serilog, CommongLogging, Catel, LibLog, MetroLog, NServiceBus and Splat, and allows you to create an adapter for your own library.

You can use Nuget to install it. Look for Anotar.*.Fody packages, for example Anotar.NLog.Fody.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Adapter for Common.Logging? I assume it itself is a facadde. – nawfal Mar 24 '16 at 06:42
  • @nawfal Yep, you're right, but I bet this is not the first time you see an abstraction layer built upon another abstraction layer (and so on and so forth). It's just the same as a logger library loggint to another logging library, like Serilog logging to NLog or Log4Net or NLog logging to ELMAH. The question should be whether it makes sense or not. – JotaBe Mar 28 '16 at 09:48
0

I somehow found all solutions in here unsatisfying, especially for libraries/APIs. Although I never thought I would, I actually wrote an Open Source "logger" which should handle most concerns.

Maybe you can have a look at Dapplo.Log, you can get it from NuGet.

The idea is to use Dapplo.Log in your library, and the project which uses this can than direct (when needed) any log output to their logger of choice. Examples for directing the output can be found in the tests, with implementations of some major frameworks here.

It's probably not perfect (yet), and I welcome pull-requests or tickets.

Robin Krom
  • 180
  • 8
0

Finding myself in this situation, I spent some time reading all answers and researching for a good solution. Most of the proposed stacks/frameworks/abstractions are now old and either not currently maintained or not popular anymore.

I then encountered LibLog, which is now deprecated, and whose ReadMe explicitly recommends Microsoft.Extensions.Logging.Abstractions "as it is now the defacto standard logging interface in the .NET ecosystem".

(For the record, Microsoft.Extensions.Logging.Abstractions is now included in .NET Runtime after it was moved there from ASP .NET Logging)

alelom
  • 2,130
  • 3
  • 26
  • 38