Database Trace Listener - ExecuteWriteLogStoredProcedure

Topics: Logging Application Block
Jan 13, 2009 at 4:37 PM
When using the Database Trace Listener and an exception occurs, I'd like to build some context around the exception by uploading the log file created using the FlatFile Trace Listener. During normal operation, I'm obviously logging more detail to the local machine trace log file, only exceptions and other special LogEntry categories (i.e. user logs on or user logs off) get logged to the database.

My thought was, I could add an Extended Property with the filepath to the LogEntry object, then extended the Database Trace Listener to look at the LogEntry object and if the extra Extended Property exists, read the file and push it into a table with a foreign key relationship to the LogId from Log table.

So I went about creating a class that inherits from FormattedDatabaseTraceListener but I can see that even if I override TraceData , I won't be able to get the int returned from ExecuteWriteLogStoredProcedure because it's marked as private.

In fact, since the database variable is private, I cannot even reuse the existing database connection, I'll have to override the constructor as well.

Is there an easier way to extend FormattedDatabaseTraceListener that I'm not seeing?
Jan 14, 2009 at 4:44 AM

On your first Item, Yes you can just add something to the extended property or you can create a Wrapper class for the LogEntry. Like this:


public class CustomLogEntry : LogEntry
public CustomLogEntry(): base(){ }
public string FilePath { get; set; }


On your second Item, You could just implement a custom trace listener. See this link:

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Jan 16, 2009 at 3:05 PM
Great idea on the first item.  Extending LogEntry to add a FilePath property.

On the second, I had dismissed that one because I don't get configuration for things like connectionstring, log proc, and category proc.  Plus I would need to reconfigure things like the private database variable.  Are you saying that Custom Trace listener can do those things?  In that simple example, I don't see any properties being passed into it.
Jan 17, 2009 at 4:41 AM
You can pass values to the custom trace listener through  the Attributes collection property. You can add values to it during design time by accessing the Attributes property of the trace listener.
You can access it through code from any method except in the constructor.
For example, you can add a key ="connectionString" and a value of = "MyDatabase" to the attributes collection.

public override void Write(string message)
      loop through the "this.Attributes" collection

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.