Using Events to Monitor Web Service Operations

One way to monitor your web service use is to take advantage of the Windows event logs to record specific events within your code. You might, for example, log information about each method invocation and completion so you can later determine how frequently methods are called and how long the methods take to complete their processing. The .NET environment makes it easy for programs and web services to place entries within the Windows event logs. The following code statements, for example, would place an entry within the Windows application log:

Dim Log As New EventLog("Application") Log.Source = "ProgramName" Log.WriteEntry("Message contents") Log.Close()

Using the Windows Event Viewer, you can later display the event-log entries as shown in Figure 14.1.

click to expand
Figure 14.1: Using the Windows Event Viewer to display event-log entries

To display entries within the Windows event logs, perform these steps:

  1. Select the Start menu Settings option and choose Control Panel. Windows will open the Control Panel window.

  2. Within the Control Panel window, double-click the Administrative Tools icon. Windows will open the Administrative Tools window.

  3. Within the Administrative Tools window, double-click the Event Viewer icon. Windows will open the Event Viewer window.

  4. Within the Event Viewer window, click the Application Log entry. The Event Viewer will display the events previously logged by programs.

Within the event log, you can scroll for events posted by the application you desire. To view an entry’s content, double-click the entry. The Event Viewer will display a dialog box that contains the event’s specifics, as shown in Figure 14.2.

click to expand
Figure 14.2: Viewing an event’s specifics within the Windows Event Viewer

Although the .NET environment makes it very easy for web services to add entries within an event log, a web service cannot create the first entry within the log. Assume, for example, you have a web service called Ecommerce and you want the service to place entries into the log each time a program calls the service. Windows will not let a web service place the initial Ecommerce entry into the log. That’s because the new log entry also requires a new entry in the Registry. After an Ecommerce entry exists, web services can use it to add other entries. The challenge becomes creating that first entry.

The Visual Basic .NET program in Listing 14.1, BuildEntry.vb, simply creates an entry within the event log. In this case, the program creates an entry named RecordEvents that you will use in the web service you create in the next section. The program (unlike the web service) can create the log entry because Windows does not restrict the program’s access to the Registry.

Listing 14.1 BuildEntry.vb

start example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ Ä System.EventArgs) Handles Button1.Click    Dim Log As New EventLog("Application")    Log.Source = "RecordEvents"    Log.WriteEntry("Created entry for RecordEvents")    Log.Close()    MessageBox.Show("Created EventLog Entry") End Sub
end example

To create the BuildEntry.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name BuildEntry. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop a button onto the form.

  4. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 14.1.

The program statements create a new EventLog object that references the Application log. Then, the program assigns the name RecordEvents as the source of the new entry. The EventLog object WriteEntry method actually places the entry into the log. Then, the program uses the Close method to indicate it is done using the log, much as a program would close a file.

Monitoring a Web Service’s Operations

In Chapter 11, “Integrating Binary Data into .NET Web Services,” you created the FileUpload web service that programs can use to upload binary (image, video, audio, and executable) files. Before you tweak the web service operations, you should study how programs use the service to determine, for example, information about the data passed to or from the methods (such as the average size of files, which will impact upload and download times).

The web service in Listing 14.2, RecordEvents, changes the FileUpload web service methods to create entries within the event log that you can use to monitor each method’s use:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name RecordEvents. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 14.2.

Listing 14.2 RecordEvents.asmx.vb

start example
Imports System.IO <WebMethod()> Public Function FileUpload(ByVal FileData() As Byte, _      ByVal Name As String) As Integer    Dim Status As Integer = 0    Dim OutputFile As FileStream    Dim FileLength As Long    Try      Dim Log As New EventLog("Application")      Log.Source = "RecordEvents"      Log.WriteEntry("Service called at " & Now.TimeOfDay.ToString() & _      Ä" with file " & FileData.Length & " bytes")         Name = "C:\Upload\" + Name      OutputFile = File.Open(Name, FileMode.OpenOrCreate, _      Ä FileAccess.Write)      FileLength = FileData.Length      OutputFile.Write(FileData, 0, FileLength)      OutputFile.Close()      Log.WriteEntry("Service complete at " & Now.TimeOfDay.ToString())      Log.Close()      Status = 1    Catch Ex As Exception      Throw Ex    End Try    FileUpload = Status End Function
end example

Note 

Remember, before the web service can place entries into the event log, you must create the first RecordEvents entry in the log, which you can do using the BuildEntry.vb program presented earlier in this chapter. To prevent network applications from having too much control of the event log, Windows will not let a web service or active server page create new source entries within the event log. Windows will, however, let them create event entries for sources that already exist.

In this case, each time a client program calls the FileUpload method, the code creates an entry in the event log stating the time the method was called and the size of the file the method is to upload. After the method completes its processing, the code places a single entry into the log that contains the current time. By comparing the two entries, you can determine the amount of time (on a per-byte or per-KB basis) the service requires to upload data.

Using a Web Service to Monitor Specific Entries

As you have learned, using the Windows Event Viewer you can display specific entries within the event logs. There may be times, however, when you must monitor specific entries within an event log from a remote PC. The EventMonitor web service in Listing 14.3 provides a method your programs can use to monitor a specific program’s events. You might, for example, use the EventMonitor web service to monitor the events placed into the log by the RecordEvents web service.

Listing 14.3 EventMonitor.asmx.vb

start example
<WebMethod()> Public Function GetEvents(ByVal TargetSource As String) _ Ä  As String()      Dim Events(1024) As String    Dim I As Integer = 0    Dim Log As New EventLog("Application")    Dim EventEntry As EventLogEntry    TargetSource = TargetSource.ToUpper()    For Each EventEntry In Log.Entries      If (TargetSource = EventEntry.Source.ToUpper()) Then        If (I < 1024) Then           Events(I) = EventEntry.Message           I = I + 1        End If      End If    Next    Dim Results(I) As String    Dim J As Integer    For J = 0 To I - 1      Results(J) = Events(J)    Next    GetEvents = Results End Function
end example

To create the EventMonitor web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name EventMonitor. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 14.3.

The code creates an EventLog object that corresponds to the Application event log. Then, the code uses the object to retrieve the log’s events. Next, the code loops through the events, assigning the events that correspond to the specified source to an array of strings, which the method returns to the caller.

The Visual Basic .NET program in Listing 14.4, ViewEntries.vb, uses the EventMonitor web service to let you monitor entries within a remote event log. When you run the program, your screen will display a form, similar to that shown in Figure 14.3, that you can use to display the events logged by a specific program or web service.

click to expand
Figure 14.3: Using the ViewEntries program to monitor entries in a remote event log

To create the ViewEntries.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program, and in the Name field type the program name ViewEntries. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text box previously shown in Figure 14.4 onto the form.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/EventMonitor/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 14.4.

Listing 14.4 ViewEntries.vb

start example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ Ä  System.EventArgs) Handles Button1.Click   Dim WS As New localhost.Service1()   If (TextBox1.Text.Length > 0) Then     Try       Button1.Enabled = False       TextBox2.Text = ""       Dim Results() As String       Dim Entry As String       Results = WS.GetEvents(TextBox1.Text)       If Results.Length = 1 And Results(0) = "" Then         TextBox2.Text = "No entries found"       Else         For Each Entry In Results            TextBox2.Text = TextBox2.Text + Entry + vbCrLf         Next       End If       Button1.Enabled = True     Catch Ex As Exception       MessageBox.Show(Ex.Message)     End Try   Else     MessageBox.Show("Must specify event-log entry")   End If End Sub
end example

After you specify the name of the source whose entries you desire and click the Get Events button, the program calls the EventMonitor web service GetEvents method to retrieve the corresponding events. The program then loops through the array that contains the event messages, assigning each message to the text box.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net