Friday, March 07, 2008

Best Practices or Recommendations for using the Logging Application Block in your .NET code

Log Message Categorization

Decide on how each message will be categorized. Once the categories are decided add them to the Category Sources node under Logging Application Block in the web.config of the application.

Examples of categories could be:

  • Performance Monitoring messages (ex:- logging the start time of a SP call and the end time of a SP call with the SP input/output parameters OR logging the start time of a page load and end time of a page load etc.)
  • Security messages (ex: - Authorization, Authentication and Auditing messages)
  • UI Event messages (ex:- user submitted form with values a, b, c etc
  • Business messages (ex:- the result of a method that does a lot of calculations , its input parameters and periodic outputs of each step in the calculation)
  • Data Access messages (ex:- creation of a database connection, closing the connection, calling an SP etc.)
  • Exception messages

    Then every message must necessarily be categorized as one and only one of the categories decided for the application before logging.

Severity Levels of Log Messages

  • Decide on the levels of severity for each message within a category. While the application block provides for 10 levels of severity (Critical, Error, Information, Resume, Start, Stop, Suspend, Transfer, Verbose, Warning), to keep things simple, it is recommended that only three severity levels are used.
  • Critical – Messages that indicate a serious systemic error that needs immediate manual intervention such as a database inaccessible OR web service inaccessible OR access to a folder that the application requires access to is denied etc.
  • Warning – Messages that indicate a potential problem in the system such as a long running SP OR a disk close to filling up etc
  • Information - Messages in this level contain contextual information. They are mostly used for problem diagnosis.

    When logging a message, apart from adding the category to the log message, also add the severity level to the log message.

Trace Listeners for the log message (or where to store the log messages)

Decide Trace Listeners to be used in the application. Typical listeners are:

  • Email – Use this for all Error severity level messages to notify someone.
  • Event Log – Use this for Warning severity level messages.
  • Flat File – Use this (or the Rolling Flat File) for Information severity level messages. Alternately, Database can be used for this severity level, especially if easy querying of the messages is required.

Decide filters (what gets logged and what doesn't)
Decide the filters that will determine what gets logged and what doesn’t. It is recommended that the following filters are added:

  • LogEnabled Filter – Set this to True
  • Category – You may set this up with two different types of filters based on the environment.
  • For production - Deny all except: Critical Messages, Warning Messages, Exceptions Generated, Performance Monitoring, Security Events is recommended.
  • For QA, staging & development - Allow all

No comments:

Post a Comment