Search This Blog

2010/01/17

Custom EventLog Creation and Manipulation:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace EventLogging_with_CSharp
{
public partial class Form1 : Form
{
string strSource;
string StrLogName;
string strEntryType;
EventLog Mylog;
EventLogEntryType EntryType;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{


strSource = textBox1.Text;
StrLogName = textBox2.Text;


Mylog = new EventLog(StrLogName);


if (!EventLog.SourceExists(strSource))
{
EventLog.CreateEventSource(strSource, StrLogName);
MessageBox.Show("Event Source Is Created");
}

Mylog.Source = strSource;



}

private void button2_Click(object sender, EventArgs e)
{
string strLogEntry;
strLogEntry = textBox3.Text;

strEntryType = listBox1.Text;
switch (strEntryType)
{
case "Information":
EntryType = EventLogEntryType.Information;
break;
case "Error":
EntryType = EventLogEntryType.Error;
break;
case "Warning":
EntryType = EventLogEntryType.Warning;
break;
case "SuccessAudit":
EntryType = EventLogEntryType.SuccessAudit;
break;

case "FailureAudit":
EntryType = EventLogEntryType.FailureAudit;
break;
}

Mylog.WriteEntry(strLogEntry,EntryType);
}


private void button4_Click(object sender, EventArgs e)
{
if (EventLog.Exists("Mylog"))
{
EventLog.Delete(Mylog.ToString());
}
}

private void button3_Click(object sender, EventArgs e)
{
if (EventLog.Exists("MyLog"))
{
Mylog.Clear();
}
}

private void button4_Click_1(object sender, EventArgs e)
{
string StrEventLogName;
StrEventLogName = textBox4.Text;
if (EventLog.Exists(StrEventLogName))
{
EventLog.Delete(StrEventLogName);
MessageBox.Show("This Event Log is Deleted");
}
}
}
}

No comments:

Post a Comment