

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
How to generate log file with Delegate and Event VB.NET
A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
This example demonstrates how to generate log file with Delegate and Event technique.
A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
Event represents the state of an event, such as the element in which the event occurred, the state of the keyboard keys, the location of the mouse, and the state of the mouse buttons.
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
First, you will need to import the System.IO namespace.
The System.IO namespace contains the StreamWriter and File Classes.
The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.
We use the button1_Click event to do the work.
Click the button Generate Log File ,System will generate a log file in folder log which locates in root directory. The code as follows.
| Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGeneratelog.Click
Client.Run(txtKey.Text.Trim(), Server.MapPath("log")) End Sub |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Add two custom class Client and UserInputMoniter in the project. The code as follows.
Imports System Imports System.IO Public Class Client
Private Shared path As String = "" Public Shared Sub Run(ByVal key As String, ByVal strpath As String)
path = strpath Dim monitor As New UserInputMoniter() Dim a As New Client(monitor) monitor.Run(key) End Sub Private Sub GenerateLog(ByVal sender As Object, ByVal e As EventArgs)
Dim writer As StreamWriter = Nothing Dim strPath As String = path + "\log.ini" Try
If File.Exists(strPath) Then
File.Delete(strPath) End If Try
writer = File.AppendText(strPath) writer.WriteLine("Test log.") Finally writer.Dispose() End Try Catch ex As Exception
Throw New Exception(ex.Message) Finally writer.Close() writer.Dispose() End Try End Sub Sub New(ByVal m As UserInputMoniter)
AddHandler m.OnUserRequest, AddressOf Me.GenerateLog End Sub End Class
Imports System
'/ <summary> '/ UserInputMoniter '/ </summary> Public Class UserInputMoniter
Delegate Sub UserRequest(ByVal sender As Object, ByVal e As EventArgs) Public Event OnUserRequest As UserRequest Public Sub Run(ByVal key As String)
If key.Trim() = "h" Then
RaiseEvent OnUserRequest(Me, New EventArgs()) End If End Sub End Class |
The front end DelegateEventVB.aspx page looks something like this:
<table>
<tr>
<td colspan="2" style="text-align: left"> Information: <span style="color: #ff3333">If the KeyValue is 'h' ,click the follow button will generate log file in the folder log which locate in root directory.</span></td> </tr> <tr>
<td style="width: 100px; text-align: right;"> <asp:Label ID="lblKeyValue" runat="server" Text="KeyValue:"></asp:Label></td> <td style="width: 100px"> <asp:TextBox ID="txtKey" runat="server"></asp:TextBox></td> </tr> <tr>
<td colspan="2"> <asp:Button ID="btnGeneratelog" runat="server" OnClick="Button1_Click" Text="Generate Log File" /></td> </tr> </table> |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The flow for the code behind page is as follows.
Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGeneratelog.Click
Client.Run(txtKey.Text.Trim(), Server.MapPath("log")) End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!