Database Logging

Topics: Logging Application Block
Jan 17, 2007 at 4:32 AM
hi all
I am trying to use the database logging without much success. My web.config
<?xml version="1.0"?>
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=null" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=, Culture=neutral, PublicKeyToken=null" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=null" />
<!-- Enterprise Logging Section begins Here-->
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="Information" logWarningsWhenNoCategoriesMatch="true">
<add databaseInstanceName="MyConnection" writeLogStoredProcName="WriteLog"
addCategoryStoredProcName="AddCategory" formatter="Flat File Text Formatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=, Culture=neutral, PublicKeyToken=null"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=, Culture=neutral, PublicKeyToken=null"
name="Primary Log Location - Database " />

<add fileName="D:\Demo.log"
header="----------------------------------------" footer="----------------------------------------"
formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=null"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=null"
name="Secondary Log Location - FlatFile " />
<add template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=null"
name="Flat File Text Formatter" />
<add categoryFilterMode="DenyAllExceptAllowed" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=null"
name="Category Filter" />
<add switchValue="Critical" name="Critical">
<add name="Primary Log Location - Database " />
<add switchValue="Error" name="Error">
<add name="Primary Log Location - Database " />
<add switchValue="Information" name="Information">
<add name="Primary Log Location - Database " />
<add switchValue="All" name="Resume">
<add name="Primary Log Location - Database " />

<add switchValue="Verbose" name="Verbose">
<add name="Primary Log Location - Database " />
<add switchValue="Warning" name="Warning">
<add name="Primary Log Location - Database " />
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings" />
<add name="General Exception Policy">
<add type="System.Exception, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="Exception">
<add logCategory="Error" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=, Culture=neutral, PublicKeyToken=null"
priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=, Culture=neutral, PublicKeyToken=null"
name="Logging Handler" />
<add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=null"
name="System.Data.SqlClient" />
<add name="MyConnection" connectionString="MyDatabase=Data Source=ITDSAPWS073\SQLEXPRESS;Initial Catalog=Logging;Integrated Security=True;Server=(local)\SQLEXPRESS;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
<appSettings />
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.

Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
<compilation debug="true" strict="false" explicit="true">
<add assembly="System.Management, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Messaging, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Data.OracleClient, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Design, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Configuration.Install, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<clear />
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
<add namespace="System.Web.SessionState" />
<add namespace="System.Web.Security" />
<add namespace="System.Web.Profile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.WebControls.WebParts" />
<add namespace="System.Web.UI.HtmlControls" />
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
<authentication mode="Windows" />
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to config
Jan 17, 2007 at 4:19 PM
Unless you are getting a specific error, it is tough to debug in a forum with just a config file.

For database logging, the logging block uses the data access application block to execute a stored procedure to save the data in the database. Therefore, you also need the DAAB to be configured properly as well.

You also need to make sure you create the database using the scripts provided, which are normally located in:

C:\Program Files\Microsoft Enterprise Library January 2006\src\Logging\TraceListeners\Database\Scripts

If you haven't already, I recommend configuring the Special Sources Logging Errors & Warnings to output to a flat file. If the Logging Application Block is having a problem using the database for logging, it will mention the error in there so you can fix the problem.

If you don't see an error in there you may not be logging to the right categories or your filters may be filtering it.




David Hayden
Microsoft MVP C#
Jan 17, 2007 at 6:14 PM
Thanks Dave, I have the database access block configured, when i open the web.config for the second time using enterprise Library config tool the connection strings seem to disappear.and the Database trace listener throws a validation errors.
I had this working before i upgrade VS2005 to SP1.
The logger does not throw any exception but does not record any database entries.

I also tried to log to the System Event.

Is the upgrade to SP1 a problem
Jan 18, 2007 at 3:35 PM
I just tried it with SP1 and it works fine.

I think the problem is your connection string shown in your config file:

connectionString="MyDatabase=Data Source=ITDSAPWS073\SQLEXPRESS;Initial Catalog=Logging;Integrated Security=True;Server=(local)\SQLEXPRESS;Integrated Security=SSPI;"


It looks like you have 2 connection strings combined, which probably explains why you lose your connection string. If you lose your connection string then your logging block will throw validation errors because the connection string is missing.

I think if you fix the connection string your problems should go away.




David Hayden
Microsoft MVP C#
Jan 30, 2007 at 2:04 PM
Thanks David,

I missed the connection string error after going thru the XML sevreal times.
My web.config is getting pretty big as I am including the sections for exception handling and logging. Can this configuration bein a seperate config file and my web.config will include this file ?

Thanks again

Jan 30, 2007 at 2:56 PM

You can have your Enterprise Library Configuration in a separate configuration file so it does not clutter up you web.config / app.config.

I talk about it in the following article:

Enterprise Library 2.0 - From Configuration Block to IConfigurationSource

Toward the end of the article I show how to configure Enterprise Library to look for all its configuration settings by default in a FileConfigurationSource as opposed to the normal SystemConfigurationSource. You will probably want to do that to avoid having to specify a FileConfigurationSource programmatically.




David Hayden
Microsoft MVP C#