How to create Database Trace listener programatically?

Topics: Building and extending application blocks, Data Access Application Block, General discussion, Logging Application Block
Nov 4, 2008 at 9:41 AM
I could log the errors using flat file & email trace listneres programatically.

How can i log the errors in database programatically, can anyone help me out with that.

Nov 4, 2008 at 1:56 PM
Edited Nov 4, 2008 at 2:04 PM
EntLib provides a script(CreateLoggingDb.cmd) for creating a database for logging.  It's located in the @\EntLib41Src\Blocks\Logging\Src\DatabaseTraceListener\Scripts folder. 

1. Run this script.
2. Add a Database Trace Listener to your configuration file.  This will automatically add the Data Access Application block if you haven't done so.
3. Create a connection string for your Logging database.
4. Set the DatabaseInstance property of your Database Trace Listener to the name of your connection string.
5. Add a category under the Category Source node and reference the Database Trace Listener.

That's it.  You'll just need to specify your log entry's category to the name of the category referencing the database trace listener.  Make sure to add references to Microsoft.Practices.EnterpriseLibrary.Logging and Microsoft.Practices.EnterpriseLibrary.Logging.Database assemblies

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
Nov 5, 2008 at 2:31 AM

Sorry, I just realized you might be asking how to do this without configuring the Logging Application block.  Of course you still need to add the necessary connection string. Here's a sample code snippet on how to do this.

TextFormatter formatter = new TextFormatter();



Database db = DatabaseFactory.CreateDatabase("LoggingDB");



FormattedDatabaseTraceListener databaseTraceListener = new FormattedDatabaseTraceListener(db, "WriteLog", "AddCategory", formatter);



LogEntry entry = new LogEntry("Testing", "Audit", 1, 1, System.Diagnostics.TraceEventType.Information, "DBLogging", null);



null, "Audit", System.Diagnostics.TraceEventType.Information, 1, entry);



Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.


Nov 5, 2008 at 3:59 AM

Thanks Sarah.

Infact, i also got solution for this same way.

//Create TextFormatrer with diffrent Tokens

template="Timestamp: {timestamp}{newline}" +
"Message: {message}{newline} " +
"Category: {category}{newline}" +
"Priority: {priority}{newline}" +
"EventId: {eventid}{newline}" +
"Severity: {severity}{newline}" +
"Title:{title}{newline}" +
"Machine: {machine}{newline}" +
"Application Domain: {appDomain}{newline}" +
"Process Id: {processId}{newline}" +
"Process Name: {processName}{newline}" +
"Win32 Thread Id: {win32ThreadId}{newline}" +
"Thread Name: {threadName}{newline}";




TextFormatter formatter = new TextFormatter(template);


//Create DatabaseTraceListener using TextFormatter created above.
string strConn - "...";

SqlDatabase sqlDB = new SqlDatabase(strConn);
FormattedDatabaseTraceListener databaseTraceListener = new FormattedDatabaseTraceListener(sqlDB, "WriteLog", "AddCategory", formatter);




//Collection of TraceListeners
LogSource errorsTraceSource = new LogSource("ErrorsTraceSource", SourceLevels.All);



// Assigning a non-existant LogSource
// for Logging Application Block
// Used to say "don't log".
LogSource emptyTraceSource = new LogSource("Empty");



//Below will log all messages with a category of "Error" or "Debug" to all TraceListeners in errorsTraceSource.
IDictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
"Error", errorsTraceSource);
"Debug", errorsTraceSource);




LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message =
"Informational message generated using Logging Application Block.";
"UI Events");
logEntry.TimeStamp =
logEntry.Severity =



LogWriter writer = new LogWriter(new ILogFilter[0], // ICollection<ILogFilter> filters
                                traceSources, // IDictionary<string, LogSource> traceSources
                                emptyTraceSource, // LogSource allEventsTraceSource
                                emptyTraceSource, // LogSource notProcessedTraceSource
                                errorsTraceSource, // LogSource errorsTraceSource
                                ErrorCategory, // string defaultCategory
                                false, // bool tracingEnabled
                                true); // bool logWarningsWhenNoCategoriesMatch





Kaushal Patel
Nomura India Services Limited.