Useful Tip – Tracing with Logging App Block


Sometimes it is useful and necessary to keep track of the activity that is going under the scenes in your application. One of the most common necessities is to have a detailed log about how much time a specific method spent to execute.

This is not a hard thing to code, but those who are already working with enterprise library can take advantage over an out of the box feature provided by it.

Here is a sample code. It´s nice and simple:

using (var tracer = new Microsoft.Practices.EnterpriseLibrary.Logging.Tracer() )
      //Put your code here

This is how it works: the timer starts when the Tracer object is instantiated and the timer stops when the object is disposed.

But, that´s not enough, we have to log the information and make it available for the people who are interested in the information. To address that, it is possible to take advantage over all of the Logging App block features available in the enterprise library.

Here is an example about how to configure it:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="false">
    <add databaseInstanceName="BAM" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Database BAM Trace Listener" />
    <add template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter" />
    <add name="Trace" switchValue="ActivityTracing">
        <add name="Database BAM Trace Listener" />
<add name="BAM" connectionString="xxx" providerName="xxx"/>

Writing the log to the database and increasing the logging capabilities in our solution we were able to create a Business Activity Monitoring (BAM) in our enterprise WCF service bus.
After that we just created a report to show the information to IT Staff.


About Rafael Tolotti

Brazilian software architect, post graduated in Strategic Management of Information Technology. Microsoft Commerce Server specialist interested in software architectures and design patterns. He's also interested in team management, risk management, project management and agile software development. As a sports lover is passionate about soccer and tennis as well.
This entry was posted in .NET Framework, Computer Science and tagged , , , , , . Bookmark the permalink.