Database trace listener and no log entries in a DB

Topics: Logging Application Block
Mar 16, 2007 at 9:22 PM
Edited Mar 16, 2007 at 9:28 PM

I'm trying to use EntLib 2.0 Logging Block. Works alright with a flat file, but I also need to log into a DB. Found a sql script that creates new DB (Logging), tables and stored procs in that new DB. Well, not really sure why someone would want to use a single DB on a DB server for all the possible applications trying to log onto that DB server with possible multiple databases there.. Anyway, so I took the SQL script from the installation and just run the part that creates tables and stored procs on my existing DB (let's call it BobDb). Now, since my Database trace listener is configured to use a connection string that connects to my BobDb and I created all the tables and stored procs in BobDb, I would expect logging to put entries in the 'Log' table in BobDb.. Well, I get no exceptions, entries are logged into a flat file just fine, but nothing appears in the BobDb.. Also, I need to add that all the tables created by the script are empty - so, nothing in Category, CategoryLog tables either..

Any ideas/help/hints? Thanks!
Mar 17, 2007 at 4:47 PM
I double checked it worked and all seems to be fine.

I am not sure of your specific issue, but I created an example that you can download from my website.

It uses Visual Studio 2005, Enterprise Library Feb 2007 CTP, Web Application Projects, and the database is located in the App_Data Directory of the website.

Hopefully this helps.




David Hayden
Microsoft MVP C#

Mar 19, 2007 at 6:37 PM
I ran into the same problem. make sure that the category that your logging with is in the Category table in the Db. There is a relationship between Log >> CategoryLog >> Category. If the category that you specify when you are logging doesn't exist then the record can not be created in CategoryLog table. Also you can look in the Error logs to see if there is some info there.

Mar 19, 2007 at 7:03 PM
Steve, thanks! I'm about to start looking into this today and will check if that is the issue.. I was actually thinking about that that's why I mentioned that my tables are empty.. Will try that.

David, I've downloaded your project and will take a look.. But I'm using v 2.0 and not sure if there is a difference in logging block between that and v. 3.0 that you were using in the example. But thanks anyway!
Mar 19, 2007 at 10:36 PM

Sorry, it was my mistake - looks like my DB user didn't have dbo privileges and I forget to explicitly grant execute permissions on the SPs.. It's all good now.


Apr 4, 2007 at 8:53 PM
Edited Apr 4, 2007 at 9:40 PM
Hi guys

I have the same problem. I even checked the read/write rights which are OK. I have all the stored procedures and tables in my database. I can run the stored procedure from Query Analyzer. But from code this is how i am doing and it is not logging anything in the db. Please somone help me. Mr David your articles have helped me a lot. Plesae help me. I am new to Here is my code.

LogEntry le = new LogEntry();
le.Message = "Testing our DB Logging";
le.EventId = 1234;
le.Title = "Database Message";
le.Priority = 1;
This is on button click. And here is my web.config. This file as per me is as perfect as it could be.

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=null" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=null" />
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="Audit" logWarningsWhenNoCategoriesMatch="true">
<add databaseInstanceName="connStringDEV" writeLogStoredProcName="WriteLog"
addCategoryStoredProcName="AddCategory" formatter="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="Database Trace Listener" />
<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="Text Formatter" />
<add switchValue="All" name="Audit">
<add name="Database Trace Listener" />
<add switchValue="All" name="General" />
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings" />
Is there any other method i need to call like ExecuteWriteLogStoredProcedure. I am confused and tired now. Please help.....

Thanks a lot in advance
Apr 4, 2007 at 10:02 PM
Try a flat file listener and see if that works?

Well, from the top of my head I don't have any other suggestions.. Not like I'm an expert in this - I take a minimum effort to make something like this work and don't care about the rest of the stuff.. So, all I have is my mistakes when trying to make this work..

1. Issues with DB privileges - make sure your DB user has proper access to all the DBs you use.. I hate to see that EntLib swallows exceptions and errors like that - no framework should do that.. But that's not the point of discussion.

2. I had issues with 'switchValue' set to something other than 'All' and was not setting LogEntry.Severity to a 'correspondent' value..

3. I DIDN'T need to insert anything into the DB to make logging work - EntLib figures that out.

4. I DID need to strip down EntLib DDL and use only those statements that create tables, relations, stored procs - I don't see any valid reason to create a single 'logging DB' per DB server, especially if you run few apps from that server..

Well, that's about it.. Not sure if this helps you, though... Good luck!

Apr 5, 2007 at 3:29 AM
I would make sure you are logging errors that might be happening in the logging application block. You aren't doing it now and it could probably tell you the problem. It is very key to log errors using the Logging Errors & Warning Special Sources. Output the errors to your event log and see what it tells you:

      <errors switchValue="All" name="Logging Errors &amp; Warnings">
          <add name="EventLog Trace Listener" />

You aren't showing your DAAB settings but just double check your named connection string, connStringDEV, is configued properly so the DAAB can get to it:

    <add name="connStringDEV" connectionString="[whatever it is]" providerName="System.Data.SqlClient" />

This is all assuming you have properly configured the database and have proper privileges. Again, log errors and warnings and see what the error says. Check SQL Profiler to verify your database is being called.




David Hayden
Microsoft MVP C#
Apr 5, 2007 at 1:53 PM
Hi guys

you are the best. I got my problem solved yesterday evening. I step into my whole code and then got to know that InsertCategoryLog was not found. But it was working fine from QA. Then i tried AddCategory from QA which gave me the same error. After spending 15 min on it i found an extra space in the name InsertCategoryLog. After removing that it is working like magic. And now i think it is simpler than i made it look like.

But David Thanks again for all your wonderful articles out there and your free sample codes. And Thanks shurik for your time and help.
Hope to get same support from you in future.