Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the best practices for logging an enterprise application? [closed]

Tags:

logging

People also ask

Why is logging an important part of a software application?

For many years, logs have been an essential part of troubleshooting application and infrastructure performance. They help provide visibility into how our applications are running on each of the various infrastructure components. Log data contains information such as out of memory exception or hard disk errors.


See the Microsoft Enterprise Library. It includes the Logging Application Block and that documentation might be what you are looking for. You can see the MSDN Documentation without having to install the library first. Specifically, see Key Scenarios and Design of the Logging Application Block to answer your question (maybe even if you are not using .NET).

UPDATE (April 23, 2013) See Embracing Semantic Logging using the new Semantic Logging Application Block


A good article is written on this at Effective logging practices ease enterprise development.

The crux is:

  1. Know the need of logging.
  2. Understand the options available and which suits you best (varies with Platform/OS/etc)
  3. Plan the strategy in advance before implementation.
  4. Keep reviewing and make the changes.

Some tips :

  • log no more than necessary in release/production
  • log error ever, if possible make this :
    • put log operation in temp var (vector/list)
    • at the end if no errors occurs, discard logs, in another case send to log file
  • create flag to control level of info to log
  • control log behavior in production/development environment
  • control concurrency access at log file
  • control how much long file log is need
  • control how much time file log should be maintained
  • sometimes is need to perform performance verification, put timers where necessary

If you really want to dive into philosophy on the subject, try "Do Not Believe in Structured Logging" on Miloslav Trmač's blog.

"...these lessons were learned the hard way..."


There are tons of logging frameworks out there including log4net and log4j, but the right one for you will depend on your platform and system.

The best logging techniques will depend largely on your platform and the design of your system. You need to know/decide how much information you need to diagnose a particular type of problem. Generally, you should instrument your code in a way that does not require a lot of code repetition or modification. Logging frameworks in .NET that use Attributed classes and methods are a good example of low-impact instrumentation. You should also be able to modify the level of logging (Low through Verbose) through configuration without having to shut down or restart any services.


Serilog is another structured logging solution that has become popular since the question was first asked.

  • Serilog website.
  • Discussion with developer Nicholas Blumhardt on .NET Rocks!

In his interview, Mr. Blumhardt talks about building the library as a result of going through the pain of logging that we have all experienced. They talk on the show about simple logging during initial development and formalizing logging as the project matures. Good stuff and may add to the best practices knowledge base brought up in the question.

From the website:

Like other logging libraries for .NET, Serilog provides diagnostic logging to files, the console, and so-on. It is easy to set up, has a clean API, and is portable between recent .NET platforms.

Unlike other logging libraries for .NET, Serilog is built with structured log data in mind. Parameters passed along with log messages are not destructively rendered into a text format. Instead, they're preserved as structured data that can be written in document form to a NoSQL data store