Database Logging not working for extended properties

Topics: Data Access Application Block
Feb 13, 2013 at 3:35 PM
I am trying to log entries in database uing Ent Lib 5.0. However, when I tried to log entries in database, it is not throwing any error, but no entries has been made in database. When I debug the program, I can get the error message in Intellitrace wind,

"Caught: "Procedure or function 'WriteLog' expects parameter '@extendedProperties', which was not supplied." (System.Data.SqlClient.SqlException) Exception Message = "Procedure or function 'WriteLog' expects parameter '@extendedProperties', which was not supplied.", Exception Type = "System.Data.SqlClient.SqlException", Exception WinRT Datp.config

This is my configuration settings in App.config file:
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
            <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="c:\trace.log" formatter="Text Formatter" />
            <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                databaseInstanceName="LoggingConnectionString" writeLogStoredProcName="WriteLog"
                addCategoryStoredProcName="AddCategory" formatter="Text Formatter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
            <add switchValue="All" name="General">
                    <add name="Event Log Listener" />
                    <add name="Flat File Trace Listener" />
                    <add name="Database Trace Listener" />
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                    <add name="Event Log Listener" />
                    <add name="Flat File Trace Listener" />
                    <add name="Database Trace Listener" />
    <dataConfiguration defaultDatabase="LoggingConnectionString" />
        <add name="LoggingConnectionString" connectionString="Data Source=servername;Initial Catalog=Logging; integrated security=True;"
            providerName="System.Data.SqlClient" />
and the related c# code is as below:
 LogEntry logEntry = new LogEntry();
            logEntry.EventId = 200;
            logEntry.Priority = 1;
            logEntry.Message = "Test Run by today - 13th Feb 2013";
            logEntry.Severity = TraceEventType.Information;
            catch (System.Data.SqlClient.SqlException sqlException)
                string msg = sqlException.Message;
How to resolve this? Because of this, the logging is not inserted into db. Please provide any help on this.
Feb 13, 2013 at 9:08 PM
Edited Feb 13, 2013 at 9:13 PM
First of all, I would recommend to not configure the errors special source to write to all of the trace listeners you have configured in the category. This is because the reason why the trace listeners in the errors special source are being called is that an error occurred trying to log to those sources and, unless the error is transient, a failure is likely again. I guess if you wanted to provide a retry mechanism the posted approach would be close. Except that (as you found out) you can never know what error occurred in the last trace listener (database trace listener) (without some sort of debugging/tracing) and sometimes in functioning trace listeners you would see the actual LogEntry followed by the corresponding error LogEntry.

I apologize if that sounds like preaching -- not my intent. :)

In terms of the error message you are seeing, the out of the box WriteLog stored procedure does not accept ExtendedProperties. It sounds like the stored procedure you are using has been modified (perhaps a version from the samples page?). I would recommend getting (or using) the out of the box database scripts as per Logging to a Database.

One more thing, is that Logger.Write will not throw a SqlException (since exceptions occurring during logging will be attempted to be written to the errors special source and then swallowed). It is possible for Logger.Write to throw but this will usually be an error related to general configuration and not to the actual logging methods.

Randy Levy
Enterprise Library support engineer
Support How-to