Three types of Runtime diagnostics:Event logging,Performance counters,Tracing

Framework provides three types of Runtime diagnostics:
1. Event logging
2. Performance counters
3. Tracing

1. Event logging
Windows 2000 and Windows XP have a feature called an Event Log _ a database containing information about important hardware or software events. The Event Log is useful for recording information about the status of your applications and provides systems administrators a means of diagnosing problems, since they can review Event Log entries using the Event Viewer (supplied with Windows and available in theAdministrative Tools group in the Control Panel). There are three types of Event Log events:
1. Informational events: Usually contain basic information, such as an application starting or shutting down
2. Warning events: Usually provide information about unusual conditions that have the potential to become errors
3. Error events: Represent critical errors in an application that prevent it from executing normally .
Events are stored in Event Logs — Windows supports three types of Event Logs:
1. Application: Contains messages that applications such as Microsoft SQL Server log
2. System: Contains messages that device drivers and system services log.
3. Security: Contains system-generated messages about events that occur when security auditing is enabled
The .NET Framework makes it easy to work with the Event Log as shown in the following
code:

Imports System
Imports System.Diagnostics
Module eventLogDemo
Sub Main()
If Not EventLog.SourceExists("ASPnetBible") Then
EventLog.CreateEventSource( _
"ASPnetBible", "Application")
Console.WriteLine( _
"Created new EventSource 'ASPnetBible'")
End If
Dim evLog As New EventLog()
evLog.Source = "ASPnetBible"
' Note: this listing does not show the
' complete message for brevity
evLog.WriteEntry( "...starting")
Console.WriteLine("Wrote 'starting'...")
evLog.WriteEntry("... exiting")
Console.WriteLine("Wrote 'exit'...")
End Sub
End Module

This code is a Visual Basic .NET console application that creates an Event Source called ASPnetBible and logs the application's starting and exiting events to the system's Application event log — although the listing doesn't show it, both messages are informational.

2. Performance counters
Performance counters are useful for monitoring the health and performance of an application. You can chart the value of using the Performance applet in the Administrative Tools folder of the systems Control Panel. The .NET Framework makes it easy for you to read the value of existing performance counters, such as the system's percent CPU Utilization, as well as create your own applicationspecific performance counters. The following code demonstrates how to work with performance counters in a simple Windows Forms application:

' Create a new performace counter

Dim counterCollection As New CounterCreationDataCollection()
Dim couterItem As New CounterCreationData()
counterName = "demoCounter"
perfCatName = "ASPnetBible"
couterItem.CounterName = counterName
couterItem.CounterType =
PerformanceCounterType.NumberOfItems32
counterCollection.Add(couterItem)
PerformanceCounterCategory.Create(perfCatName, _
"sample counter", counterCollection)
' ...elsewhere in the application - Increment the counter
Dim perfCounter As PerformanceCounter
perfCounter = New PerformanceCounter()
perfCounter.CategoryName = perfCatName
perfCounter.CounterName = counterName
perfCounter.ReadOnly = False
perfCounter.IncrementBy(50)
System.Threading.Thread.Sleep(2000)
perfCounter.IncrementBy(-50)
'...elsewhere in the application - Delete the sample counter
PerformanceCounterCategory.Delete(perfCatName)

This code demonstrates how to create a new performance counter category and counter using the CouterCreationDataCollection and CouterCreationData classes — the fragment shown is from the sample application's Load event handler. In the next section of the code, from a button's Click event handler, the code creates an instance of the sample performance counter, increments it, and waits two seconds before decrementing the counter. The last part of the code shows how to delete the performance counter when the form closes.

3. Tracing
Debugging an application by using the Visual Studio .NET debugger is a great way to track down problems; however, there are many scenarios in which things happen too quickly to follow interactively or in which you simply need to know the sequence of events that lead to a problem before the problem occurs. Tracing is an alternative to using a debugger to step through each line of code as your application executes. You can configure ASP.NET tracing by using two methods: pagelevel tracing and application-level tracing. Both types of tracing provide similar results; however, the difference is in how you access the results for each approach. Page-level tracing provides trace details on the ASPX page when it completes executing, and application-level tracing stores the details of the trace in a file called (by default) trace.acx, which is located in the same directory as the ASP.NET application — you can view the file by using your browser. When you enable tracing, which is disabled by default, ASP.NET records detailed information about the page request, trace messages, control information, cookies, header information, the contents of any form fields, and a raw output of the contents of server variables (like CONTENT_TYPE and HTTP_REFERRER). Table 1-1 shows a fragment of a trace output from a simple ASP.NET page.
Table 1-1: Fragment of an ASP.NET Page Trace
Category Message From
First(s)
From
Last(s)
aspx.page Begin Init
aspx.page End Init 0.000096 0.000096
aspx.page Begin
LoadViewStat
e
0.000189 0.000092
aspx.page End
LoadViewStat
e
0.000308 0.000119
aspx.page Begin
ProcessPostD
ata
0.000393 0.000085

0 Comments: