

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
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.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
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 void Button1_Click(object sender, EventArgs e) {
Client.Run(txtKey.Text.Trim(),Server.MapPath("log")); } |
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!
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()); } } } |
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> |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
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!