Logging config data in our application
Now that we have the
collection of diagnostic data configured, we need to add code to our
application to send diagnostic data to the listeners. We can do this
simply by making calls to the methods in the System.Diagnostics.Trace class (documented at http://msdn.microsoft.com/en-us/library/36hhw2t6.aspx).
One of the more common methods we'll call is Trace.Writeline, as seen here:
Trace.Writeline("An error has occurred!","Error")
If our filter is set to the value Error or higher, our message would be logged. An alternative, easier syntax is:
Trace.TraceError("An error has occurred!")
Again, if our filter is set to the value Error or higher, our message will be logged. The simplified methods are limited to TraceError, TraceInformation, and TraceWarning, whereas the WriteLine method can be used to log diagnostic data at any level, including custom levels.
Transferring and persisting diagnostic data
As diagnostic data are
logged, the data are buffered in memory. In order for us to retain the
data for analysis, we need to make sure that the data persists in a
proper storage container. This is not set up by default, but it takes
only a couple of lines of code to configure the transfer of data into
the storage location. We can set this transfer to be either scheduled,
or on demand.
To automatically transfer the diagnostic data on a schedule, we just need to add a single line to our role's OnStart method:
diagConfig.PerformanceCounters.ScheduledTransferPeriod = System.TimeSpan.FromMinutes(1.0)
The entire OnStart method for our web role now reads like this:
Public Overrides Function OnStart() As Boolean
OnStart methodDim diagConfig As DiagnosticMonitorConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration()
Dim procTimeConfig As PerformanceCounterConfiguration = New PerformanceCounterConfiguration()
procTimeConfig.CounterSpecifier = "\Processor(*)\% Processor Time"
procTimeConfig.SampleRate = System.TimeSpan.FromSeconds(1.0)
diagConfig.PerformanceCounters.DataSources.Add(procTimeConfig)
diagConfig.PerformanceCounters.ScheduledTransferPeriod = System.TimeSpan.FromMinutes(1.0)
DiagnosticMonitor.Start("DiagnosticsConnectionString")
Return MyBase.OnStart()
End Function
We'll need to add similar code to the OnStart methods of the other roles in our application if we want to automatically transfer the diagnostic logs for those roles too.
An on-demand transfer is a
little different an on-demand transfer can be initiated either from
within the role, from another role in the same application, or even a
completely different application. To reduce the amount of diagnostic
data we need to sort through when debugging, we might want to log every
level of diagnostic data but transfer the diagnostic data only when an
error occurs.
Public Sub TransferDiagnosticData()
Dim diagManager As DeploymentDiagnosticManager = New DeploymentDiagnosticManager(<Azure storage account name>, <Azure deployment ID>)
dim roleInstDiagMgr as RoleInstanceDiagnosticManager = diagManager.GetRoleInstanceDiagnosticManager( <Role name>, <Role instance name>)
Dim dataBuffersToTransfer As DataBufferName = DataBufferName.Directories
Dim transferOptions As OnDemandTransferOptions = New OnDemandTransferOptions()
With transferOptions
.From = DateTime.MinValue
.To = DateTime.UtcNow
End With
Dim requestID As Guid = roleInstDiagMgr.BeginOnDemandTransfer(dataBuffersToTransfer, transferOptions)
End Sub
Complete documentation for transferring buffered data to storage can be found at http://msdn.microsoft.com/en-us/library/ee830425.aspx.
It's
important to note that diagnostic data is treated the same as any other
data associated with our application, and we will be charged for the
storage of the diagnostic output.