logging block, database listener

Topics: Logging Application Block
May 8, 2012 at 7:59 PM
Edited May 8, 2012 at 8: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();
            builder.ConfigureLogging().LogToCategoryNamed(categoryName)
                .SendTo.Database("Formatted Database TraceListener")
                .UseDatabase("Logging")
                .WithAddCategoryStoredProcedure("AddCategory")
                .WithWriteLogStoredProcedure("WriteLog")
                .FormatWith(new FormatterBuilder().TextFormatterNamed("Text Formatter"));


                builder.ConfigureData()
                    .ForDatabaseNamed("Logging")
                    .ThatIs.ASqlDatabase()
                    .WithConnectionString(@"Data Source=54L47R1\MAXIMUSPET;DataBase=Logging;Integrated Security=True;")
                    .AsDefault();

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

May 8, 2012 at 10: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
entlib.support@live.com 

May 9, 2012 at 2:43 PM

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