You need a database TraceListener to be configured at the first place. Following which, configure the logging block to log the messages to the database, similarly the exception handling block to log exceptions to the database. And when an error occurs, use the logging category to log exceptions to the database
There are a good amount of database scripts that you will have to configure to make this work.
As per the config settings, you will have to first add the dataconfiguration section to the ConfigSections node. See below.
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" requirePermission="true"/>
</configSections>
Add a Listener to the logging configuration listeners that can write to the database.
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedData baseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database" databaseInstanceName="LoggingDatabase" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"/>
</listeners>
</loggingConfiguration>
Add a db policy in the exception policies under Exceptionhandling.
<exceptionHandling>
<exceptionPolicies>
<add name="DB Policy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None">
<exceptionHandlers>
<add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging" logCategory="DBCategory" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority="0"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
Also add the dataconfiguration tag, which points to the database.
<dataConfiguration defaultDatabase="LoggingDatabase"/>
Note that Loggingdatabase in above node would correspond to a connection string named logging database, like below:
<connectionStrings>
<!-- Logging Database -->
<add name="LoggingDatabase" connectionString="Data Source=14.23.35.233;Initial Catalog=Logging;Persist Security Info=True;User ID=ejoi;Password=*****;" providerName="System.Data.SqlClient"/>
</connectionStrings>
And in your generic error handler, point to Db policy
ExceptionPolicy.HandleException(ex, "DB Policy");
This should suffice. Thanks