28

I'm looking at switching to a new unified logging solution for use in our new line of products and I wanted to see what some of the people on Stack Overflow thought. We will need logging for a variety of applicatons: ASP .net, windows services, web services, wpf apps etc. We are a windows shop only.

Some of our requirements for a logging solution include:

1) Log file management

- Ability to split files up over a certain size
- Ability to auto archive/delete after certain period of time

2) Ability to send emails on certain types of messages logged (errors for example)

3) Ability to write messages to the windows event log

- We need to be able to specify where it's being written in the event log. 
  It would also be nice if it would automatically create the event log source if it does exist.

I've started looking at nLog, windows trace and log4net. I'm not limited to these 3 only it's just a few that came up a lot when searching.

Cole W
  • 15,123
  • 6
  • 51
  • 85
  • 1
    log4net is by far the most commonly used one, and supports all of these requirements. It's the one I would use, but YMMV. – Thorarin Jan 12 '11 at 16:07
  • 5
    Try this: http://stackoverflow.com/search?q=[.net]+logging You will find plenty of interesting answers. – Ladislav Mrnka Jan 12 '11 at 16:09
  • Duplicate, but Cole W does put forward some specific requirements he thought of, which is nice about this question. – Marijn Jan 12 '11 at 16:17

7 Answers7

18

log4net is always a good choice.

http://logging.apache.org/log4net/

RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • 3
    No, it's not a good choice if you want something easy to implement. Sure, if you're a log4net user already, it may be easy for you. For anyone who's never used it before, setting it up is a pain in the ass. – Pedro Jan 13 '11 at 16:49
  • 1
    Less of a pain in the ass than writing your own logging framework methinks! – RichardOD Feb 22 '11 at 15:50
  • 1
    @Pedro what are you *implementing*? You can find app.config examples for how to setup the logger. – Stealth Rabbi Nov 02 '11 at 20:13
  • Log4 is very easy to 'implement' if you are willing to RTFM. – gbtimmon Jun 19 '13 at 14:54
  • @Pedro Do you have an alternative suggestion? – JHixson Jan 27 '14 at 16:53
  • Of late, I've been using Serilog: http://serilog.net/ It's easy to use out of the box and supports structured logging (logging more than just text). – RQDQ Feb 26 '16 at 21:13
11

Use .NET common logging. You can choose later a specific provider (NLog, CLog, log4net...), or even create custom ones.

HuBeZa
  • 4,715
  • 3
  • 36
  • 58
  • 4
    But what if I want to avoid a dependency on .NET common logging? Honestly, this seems like an unnecessary layer of abstraction to me. log4net et al are themselves abstractions over logging - what possible reason could I have for wanting to swap one for another that isn't also an argument against using .NET common logging? – Kent Boogaart Jan 12 '11 at 16:26
  • 7
    This is similar to apache commons logging for Java, and the same argument applies for it's existence. Let's say you are writing libraries that will be used in other code. You do not want to dictate which logging framework is used in any code that interacts with your library, because that means the client has to have config for your particular framework. Say if you decide to use log4net, another library uses NLog then the client has to have both of those configs present to use both libraries. Commons allows you to inherit the logging framework from the client code. – Ceilingfish Jan 12 '11 at 16:47
10

And just another one: NLog.

  • Files – single file or multiple, with automatic file naming and archival
  • Event Log – local or remote Database – store your logs in databases supported
  • by .NET Network – using TCP, UDP, SOAP, MSMQ protocols
  • Command-line console – including color coding of messages
  • E-mail – you can receive emails whenever application errors occur
  • ASP.NET trace
  • and many more
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
  • I used NLog in my previous project, and found it really easier to configure than Log4Net (which wouldn't work, inexplicably). – Shimrod Jan 12 '11 at 16:13
3

You can take a look at Enterprise Library, they have an application block for logging which is extensible

http://entlib.codeplex.com/

http://entlib.codeplex.com/releases/view/46741

you can download the dev guide pdf, they have a section on logging (Chapter 4)

pdiddy
  • 6,217
  • 10
  • 50
  • 111
  • 1
    we had *very* bad experiences with that one - on web servers, it logged and locked the file (that is, it didn't release it in time for another log), so the next log went to other file that had GUID as a name... very impractical for any production environment... – veljkoz Jan 12 '11 at 16:16
  • 1
    It sounds like you were trying to log to the same file from multiple processes (or using multiple `TraceListeners` that point to the same file). See: http://stackoverflow.com/questions/1728571/logging-from-multiple-processes-to-same-file-using-enterprise-library-4-1 . I've been using Enterprise Library in enterprise production environments since it was released and I have had very good experiences. – Randy Levy Jan 13 '11 at 18:59
1

See this question for another comprehensive answer: When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

In short, the main logging frameworks available are the built-in .NET Framework System.Diagnostics, log4net, NLog and Enterprise Library Logging Application Block.

A comparison of these main frameworks is available at: https://essentialdiagnostics.codeplex.com/wikipage?title=Comparison

1) Log file management

All the main frameworks listed above support rolling files but I think they leave the cleanup to you.

e.g. in the past I've used a scheduled Windows job that uses robocopy with with "/mov /minage X" to move old files elsewhere, then delete or whatever.

The EventSchemaTraceListener used in System.Diagnostics, which I blogged about recently, has a LimitedCircularFiles option, but not much tool support for viewing the logs (they are in XML).

2) Ability to send emails on certain types of messages logged (errors for example)

All the main frameworks listed above support this either directly or through extensions (additional listeners).

3) Ability to write messages to the windows event log

Again, all of the main frameworks support this, however I generally would recommend that writing to the Windows event log should be done directly and not via tracing.

One of the issues is what you asked about for automatic creation of sources.

Any writing to the event log (that goes through EventLog.WriteEvent) will automatically attempt to create the source on the first log message if it doesn't exist -- the problem is with security, only administrators are allowed to create sources, so running as a normal user this fails.

Because of this you really do need to add an EventLogInstaller, so that the source is created at install time (installation is done by administrator). Once created, any process can then write to the source.

This then leads to my recommendation that you need to create and write to the event log in code in order to ensure the event log source is the same. Also, if writing to the event log generally you don't want to be able to 'turn it off' through misconfiguration.

My personal recommendation is for the out of the box .NET Framework System.Diagnostics, in particular the Service Trace Viewer is excellent for diagnosing issues, particular in multi-tier, multi-threaded environments if using WCF where it can pass correlation identifies across tiers.

Community
  • 1
  • 1
Sly Gryphon
  • 3,751
  • 1
  • 25
  • 17
0

http://alexworx.github.io/ALox-Logging-Library/

is a pretty straight forward library which is great if you have high demands on performance.

Remi Smirra
  • 2,499
  • 1
  • 14
  • 15
0

You can create your own logging lib... I did.

Take a look at PostSharp http://www.sharpcrafters.com/ they have very good examples of some basic logging systems.

Salvador Sarpi
  • 981
  • 1
  • 8
  • 19