2

I'm really new to logging, and I've decided it's time to take the plunge and start working it into my applications from now on.

I'm going to be using NLog as I hear it's very fast and simple to set up.

What type of information should I be concerned about logging? What information should be considered superfluous and unnecessary?

On a more specific line, where should I be placing my _logger.Info("Foo"); calls? Within each ActionMethod in my controllers?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257

4 Answers4

2

You should look at ELMAH It is the easiest thing to setup using nuget. And it provides awesome logging for asp.net MVC (logs all exceptions and the request associated with it), all the effort you have to do is to install the package in your application. :)

http://code.google.com/p/elmah/

MoXplod
  • 3,813
  • 6
  • 36
  • 46
  • not entirely - mvc requires a bit more to properly catch everything. – Adam Tuliper Sep 30 '11 at 03:19
  • I agree, but for a person just starting, this gives a good amount of logging. Obviously he should start handling his own errors and logging it properly as a next step. – MoXplod Sep 30 '11 at 07:08
0

The importance of logging cannot be explained in a word or two.

Logging forms an essential part of investigation by your application support L2 and L3 teams. They need the logs to respond to what has happened, bugs and so on.

Since logging forms non functional concern of any application, it is a cross cutting concern in terms of Aspect Oriented Programming, many aop frameworks like aspectJ is used in the Java development wing to implement logging, together with mature loggers.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
0

http://www.dotnetlogging.com/ provides good tools for logging in .net

SaQiB
  • 639
  • 8
  • 18
0

We use log4net and ELMAH. ELMAH is used for anything top level or unhandled.

We log (log4net) every method. We wrote a macro to inject logging enter/exit code (although there is a product out there that will inject this instrumentation in your compiled code)

For other steps we log out debug info (loading record id 15) log.Debug(...) other times info "processing customer JOHN' using log.Info(..) when we catch an exception we log.Error()

Then we deploy our application and set our logging level to debug for the first bit of time, then switching over to "info" once its running smoothly.

This provides quite a bit of detail, but its up to you to decide where. Im just saying we log details in virtually every method.

also remember when you catch exceptions and log them, do now rethrow them. IE DO NOT

throw ex;

just simply

throw;

log4net can be found here: http://logging.apache.org/log4net/ and a nuget package is available for automatic configuration at http://nuget.org/List/Packages/log4net

ELMAH is available via nuget as well for automatic configuration although there are extra steps you need to take to work with MVC. Check out: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71