logging block, database listener

Topics: Logging Application Block
May 8, 2012 at 8:59 PM
Edited May 8, 2012 at 9:02 PM

Question concerning logging. 



I would like to change the structure of the Logging tables to use Guids instead of int identity as the primary key.  The Stored procedures currently return a new identity value.  Is this necessary for the loggin block?  For instance, does the .WithAddCategoryStoredProcedure("AddCategory") expect an integer to be returned from the AddCategory Stored Procedure?

 the following works:

             // Log the message
            ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
                .SendTo.Database("Formatted Database TraceListener")
                .FormatWith(new FormatterBuilder().TextFormatterNamed("Text Formatter"));

                    .WithConnectionString(@"Data Source=54L47R1\MAXIMUSPET;DataBase=Logging;Integrated Security=True;")

            DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
            using (var writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>())
                writer.Write(message, categoryName);

May 8, 2012 at 11:51 PM

The AddCategory stored procedure returns the categoryID of the category but this value is ignored in the code.  

However, the WriteLog stored procedure uses the integer LogID of the newly inserted record and returns that as a return value and an output parameter.  The FormattedDatabaseTraceListener extracts the output parameter and converts it to an int.  This LogID int value is then used to insert an entry in the CategoryLog table.

So it looks like you cannot use the FormattedDatabaseTraceListener and still maintain the category information.  You could switch the LogID column to be a UNIQUEIDENTIFIER but you would still need to set an int value because the code is expecting it (and the AddCategory stored procedure expects an int value as Log ID).

If you wanted everything to work together 100% with GUID values then you should probably create a new Trace Listener to do exactly what you want.  See Extended Properties Trace Listener with Custom Exception Handler on the Samples page for a custom database trace listener (to modify).

If you don't care about the category you could hard code an int value in the WriteLog stored procedure and have the AddCategory stored procedure ignore the logID value

Randy Levy
Enterprise Library support engineer

May 9, 2012 at 3:43 PM

Thanks for the response.  I appreciate the clear and concise answer.