The Listener allows to write trace messages into Azure Queue Storage or Azure Table Storage. While Table Storage is more useful to store data for longer time, the Queue Storage allows to collect trace messages almost in real time, which makes it particularly handy for debugging and test purposes.

The trace listeners can be used with either local development storage while in development and local debug mode, or using real cloud storage both from local and cloud deployments.

Configuration and Deployment

Configure the trace listener as you would do with any other trace listener, typically in the web.config or app.config of the application. For deployment, make sure the trace listener can be found and loaded at runtime. In Azure, this means you will have to deploy the trace listener library with your deployment package. Best way to do is to add a reference in your project to the trace library, so the Azure Package handler can do the magic.

For configuration, it’s particularly important to provide the credentials for the Azure storage account to be used, and set the “UseDevStorage” attribute to false for deployment to cloud. All attributes are described in the configuration section.

<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add type="CloudTrace.QueueTraceListener, CloudTrace, Version=1.7.0.0, Culture=neutral, PublicKeyToken=50ba629619a987d5" name="QueueListener"
             initializeData="useDevStorage=true,queueName=cloudlogging,async=false,account=XXX,accessKey=XXX"
          queueName="cloudlogging" async="true" usePlainText="false">
        </add>
        <add type="CloudTrace.TableTraceListener, CloudTrace, Version=1.7.0.0, Culture=neutral, PublicKeyToken=50ba629619a987d5" name="TableListener"
               initializeData="useDevStorage=true,tableName=cloudlogging,async=false,account=XXX,accessKey=XXX"
            tableName="cloudlogging" async="false">
            <filter type=""/>
        </add>
        <remove name="Default" />
        </listeners>
    </trace>
  </system.diagnostics>

 Using the Nuget-Package, a standard config will be supplied to web.config or app.config adding a QueueTraceListener which is targeting the (local) Azure Development Storage.

In the code, trace messages can be created using the Trace or TraceSource classes from System.Diagnostics namespace

 

Trace.Write(“some trace message”);

Attributes

account The Windows Azure Storage account to be used. For instance on “https://mystorageaccount.queue.core.windows.net/” the account would be “mystorageaccount”
accessKey The access key for the given storage account. This is usually copied from the Azure management portal.
useDevStorage If this attribute is set to true, the trace listener will use local development storage instead the storage account given.
queueName The name of the queue to be used by the queue listener. If not exists, the queue will be created.
tableName The name of the table to be used by the table listener. If not exists, the table will be created.
usePlainText If set to true, trace messages written to the queue will be clear text, thus could be read by other Azure Storage managers as well. If false, a binary serialization format is used. The Trace library provides both serializing and deserializing functionality for use with own applications.
async If set to true, storage operations for Azure Storage will use async pattern. This provides a better performance on average, however does not guarantee messages to be written. E.g. when an application crashes and you need to know the latest status which has been traced, you should set this to false.


 

Viewer

The Trace Listener Library contains basic samples to read the logs as well. Beside a simple console based reader, there is also a Windows version which reads all existing messages from a queue, so you can get started immediately.

 

Screenshot1 

To use the reader, simply provide the storage details such as account and access key as well as the queue name. Make sure the Reader uses the same format (usePlainText = true|false) as when you configured the Trace Listener.

<configuration>
  <connectionStrings>
    <add name="DataConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=XXX" />
  </connectionStrings>
  <appSettings>
    <add key="queueName" value="cloudlogging"/>
    <add key="usePlainText" value="false"/>
  </appSettings>

 

To read data from development storage, simply point to development Storage:

configuration>
  <connectionStrings>
    <add name="DataConnectionString" connectionString="UseDevelopmentStorage=true" />
  </connectionStrings>
  <appSettings>
    <add key="queueName" value="cloudlogging"/>
    <add key="usePlainText" value="true"/>
  </appSettings>

Last edited Aug 13, 2012 at 6:48 AM by xidar, version 3

Comments

xidar Jul 9, 2012 at 6:54 PM 
good point, I'll double check, also the Nuget Package which brings some default config as well. Thanks for notifying!

gabba Jul 9, 2012 at 2:41 PM 
Don't forget to actualize Version and PublicKeyToeken to make it works
"add type="CloudTrace.QueueTraceListener, CloudTrace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=50ba629619a987d5" name="QueueListener""