New to logging in general, but what should I log in an MVC3 application?
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 开发者_如何学Cplacing my _logger.Info("Foo");
calls? Within each ActionMethod in my controllers?
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/
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.
http://www.dotnetlogging.com/ provides good tools for logging in .net
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?
精彩评论