Multiple Database Trace Listener for logging

Topics: Logging Application Block
Dec 19, 2013 at 10:55 PM
Hi is it possible to have multiple database trace listners and applying the filters to distribute the errors, warnings, information, critical messages among the different databases?
Dec 26, 2013 at 4:30 PM
Edited Dec 26, 2013 at 4:31 PM
You should be able to achieve what you want (although it depends on the specifics of your situation).

The first step is that you will need to be able to distinguish between LogEntry's so that they can be sent to the appropriate database. To do this you could use the LogEntry Category.

So you could have one category that sends LogEntry's to database1 and another that sends LogEntry's to database2. The XML configuration could look like this, for example:
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="Database1 Category">
        <listeners>
            <add name="Database Trace Listener1" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                databaseInstanceName="Connection String" writeLogStoredProcName="WriteLog"
                addCategoryStoredProcName="AddCategory" />
            <add name="Database Trace Listener2" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                databaseInstanceName="Connection String" writeLogStoredProcName="WriteLog"
                addCategoryStoredProcName="AddCategory" />
        </listeners>
        <categorySources>
            <add switchValue="All" name="Database1 Category">
                <listeners>
                    <add name="Database Trace Listener1" />
                </listeners>
            </add>
            <add switchValue="All" name="Database2 Category">
                <listeners>
                    <add name="Database Trace Listener2" />
                </listeners>
            </add>
        </categorySources>
    </loggingConfiguration>

If the rules for distributing the LogEntry's are more complicated and don't map into predefined categories then you could use a programmatic approach to create a LogWriter for each destination and then write a method to retrieve the appropriate LogWriter. This answer shows an Enterprise Library 6 approach (using category but could be extended with some custom logic): http://entlib.codeplex.com/discussions/450137 .

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by birarich on 12/30/2013 at 1:12 PM
Dec 30, 2013 at 9:12 PM
Thanks Randy Levy....