This project has moved and is read-only. For the latest updates, please go here.

SqlDatabaseTraceListener Class

Trace listener that writes to a database.


Install via NuGet:


This listener writes to the database table created by the diagnostics_regsql tool (included in the NuGet package), via the stored procedure created by the tool. You can run the tool to automatically create the table and stored procedure for you.

Alternatively you can supply your own command text, which can use the following SQL parameters: @ApplicationName, @Source, @Id, @EventType, @UtcDateTime, @MachineName, @AppDomainFriendlyName, @ProcessId, @ThreadName, @Message, @ActivityId, @RelatedActivityId, @LogicalOperationStack, @Data.

If you supply your own command then you need to create any tables and stored procedures yourself.

Config Attributes

Attribute Description
initializeData Name of the connection string of the database to write to.
traceOutputOptions Not used.
applicationName Application name to use when writing to the database; set this value when the database is shared between multiple applications. The default value is an empty string.
commandText Command to use when calling the database. The default is the diagnosticsTraceAddEntry stored procedure created by the diagnostics_regsql tool.
maxMessageLength Maximum length of the message text to write to the database, where the database column has limited size. Messages (after inserting format parameters) are trimmed to this length, with the last three characters replaced by "..." if the original message was longer.

Example Config

<?xml version="1.0" encoding="utf-8" ?>
    <add name="diagnosticsdb"
      connectionString="server=.;database=diagnosticsdb;Integrated Security=SSPI" />
      <add name="sqldatabase"
        type="Essential.Diagnostics.SqlDatabaseTraceListener, Essential.Diagnostics.SqlDatabaseTraceListener"
        applicationName="Diagnostics.Sample" />
      <source name="ExampleSource" switchValue="All">
          <clear />
          <add name="sqldatabase" />

Example Output

Use the diagnosticsregsql tool to create the diagnosticsTrace table and stored procedure:


Data is written to SQL using the specified command.

You can query the diagnostics_Trace table to see the messages written:

SqlDatabaseTraceListener Example Output

Config Template

<add name="sqldatabase"
  type="Essential.Diagnostics.SqlDatabaseTraceListener, Essential.Diagnostics.SqlDatabaseTraceListener" 
  initializeData="connection string name"
  applicationName="application name"
  commandText="SQL command"

Last edited Feb 8 at 12:19 AM by sgryphon, version 6


No comments yet.