Tuesday, 19 July 2011

How to generate log file with Delegate and Event C#


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.

First, you will need to import the System.IO namespace.

The System.IO namespace contains the StreamWriter and File Classes.

using System.IO

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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 void Button1_Click(object sender, EventArgs e)
{
Client.Run(txtKey.Text.Trim(),Server.MapPath("log"));
}

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Add two custom class Client and UserInputMoniter in the project. The code as follows.

using System;
using System.IO;

public class Client
{
static string path = "";
public static void Run(string key,string strpath)
{
path = strpath;
UserInputMoniter monitor = new UserInputMoniter();
new Client(monitor);
monitor.Run(key);
}

private void GenerateLog(object sender, EventArgs e)
{
StreamWriter writer = null;
string strPath = path + "\\log.ini" ;
try
{
if (File.Exists(strPath))
{
File.Delete(strPath);
}
using (writer = File.AppendText(strPath))
{
writer.WriteLine("Test log.");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
writer.Close();
writer.Dispose();
}
}

Client(UserInputMoniter m)
{
m.OnUserRequest += new UserInputMoniter.UserRequest(this.GenerateLog);
}
}

using System;

/// <summary>
/// UserInputMoniter
/// </summary>

public class UserInputMoniter
{
public delegate void UserRequest(object sender, EventArgs e);
public event UserRequest OnUserRequest;

public void Run(string key)
{
if (key.Trim() == "h")
{
OnUserRequest(this, new EventArgs());
}
}
}

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 front end DelegateEventCsharp.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>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Client.Run(txtKey.Text.Trim(),Server.MapPath("log"));
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment