Get Logging in database with the Enterprise Library 4.1

Applications generate logs to keep track of their health, to assist in debugging, and for auditing purposes. Logging makes applications more supportable by giving developers a standardized method for recording application information. The Logging Application Block is designed to support flexible and robust logging configurations.

The heart of the Enterprise Library Logging Application Block is its TraceListeners, which are essentially the conduit for which event messages get to their intended destinations. Enterprise Library 2.0 provides the following TraceListeners:

  • Database TraceListener
  • Email TraceListener
  • Flat File TraceListener
  • Formatter Event Log TraceListener
  • Msmq TraceListener
  • System Diagnostics TraceListener
  • WMI Trace Listener

The usefulness of each TraceListener is fairly obvious. You would use the Database TraceListener to log trace messages to a database. The Email TraceListener is for sending trace messages via email.

Many companies require audit logging, especially for applications that require history records (or audit trails) of operations performed on sensitive information. This type of logging tends to contain larger amounts of information. In some cases, it may include entire database records. If the auditing is for security purposes, it tends to capture previous and current state as well as who made the change.

Logging to a database makes it easy to write queries against the logged messages for troubleshooting purposes. The logging block comes bundled with an out-of-the-box database trace listener that is capable of writing to a data store.

Lets see then how can we configure database logging:

To use the database-driven logging, you first need to execute a SQL script (supplied with Enterprise Library) to create the logging database. Run the LoggingDatabase.sql file that you'll find in the C:\EntLib41Src\Blocks\Logging\Src\DatabaseTraceListener\Scripts folder. This will create a database named Logging in your SQL Server along with the required tables and stored procedures.

Add a connection string for it, in addition to your default database:



Using the name of the logging database connection create a logging configuration tag in web.config as follows



As seen, The logging block configuration schema consists of four key elements named Category Sources, Special Sources, Trace Listeners, and Formatters. By default, the logging application block creates a Formatted EventLog TraceListener entry and configures it as the category source with the Text Formatter as the default formatter.

Then generate an exception and you will see a error record in the log table.

Here are some great links to start with Microsoft Enterprise Library

http://msdn.microsoft.com/en-us/library/cc467894.aspx

http://www.codeplex.com/entlib

धन्यवाद |

4 comments:

MG said...

Awesome. It really helped... Thanks.

RT said...

I create a page with only statement of "int error = int32.parse("abc");"

No record is logged. Could you post an example?

Vinit said...

Raymond,

Record would be logged on if any exception is there. Why don't you throw an exception there using Exception policy of EL?

Vinit said...

throw an exception and catch this way

catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, GlobalConst.ExceptionPolicy);
}

I hope this helps.