Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Event Viewer read, write and clear in C#
http://3.bp.blogspot.com/-dDqxE4V5vTw/T9IWQSRQyYI/AAAAAAAABhM/hnejenW1xic/s640/EventViewer_Read_Write_Clear_C%23.jpg
//Event Viewer read, write and clear in C#
using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
namespace EventViewer
{
public partial class EventViewer : Form
{
public EventViewer()
{
InitializeComponent();
}
///
/// Read the Logs from Event Viewer - Application
///
public void ReadLogs()
{
//logType can be Application, Security, System or any other Custom Log.
string logType = "Application";
DataTable dtable = new DataTable("EventViewer");
DataColumn column;
DataRow row;
int i=0;
int LastLogToShow = 0;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "InstanceID";
column.ReadOnly = true;
// Add the Column to the DataColumnCollection.
dtable.Columns.Add(column);
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Source";
column.ReadOnly = true;
// Add the Column to the DataColumnCollection.
dtable.Columns.Add(column);
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "EntryType";
column.ReadOnly = true;
// Add the Column to the DataColumnCollection.
dtable.Columns.Add(column);
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.DateTime");
column.ColumnName = "Date";
column.ReadOnly = true;
// Add the Column to the DataColumnCollection.
dtable.Columns.Add(column);
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Message";
column.ReadOnly = true;
// Add the Column to the DataColumnCollection.
dtable.Columns.Add(column);
try
{
EventLog ev = new EventLog(logType, System.Environment.MachineName);
LastLogToShow = ev.Entries.Count;
if (LastLogToShow <= 0)
{
dgvShow.DataSource = null;
MessageBox.Show("No log in the Event Viewer");
return;
}
for (i = ev.Entries.Count - 1 ; i > 0; i--)
{
EventLogEntry CurrentEntry = ev.Entries[i];
row = dtable.NewRow();
row["InstanceID"] = CurrentEntry.InstanceId;
row["EntryType"] = Convert.ToString(CurrentEntry.EntryType);
row["Date"] = Convert.ToDateTime(CurrentEntry.TimeGenerated);
row["Source"] = Convert.ToString(CurrentEntry.Source);
row["Message"] = Convert.ToString(CurrentEntry.Message);
dtable.Rows.Add(row);
}
ev.Close();
if (dtable.Rows.Count > 0)
{
dgvShow.DataSource = dtable;
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// EventView Click Event - Used to bind the Log message in the grid
///
///
///
private void btnEventViewer_Click(object sender, EventArgs e)
{
ReadLogs();
}
///
/// Write the log in the Eventviewer
///
void WriteLog()
{
try
{
//See if the source exists.
if (!(EventLog.SourceExists("MySystemSource", System.Environment.MachineName)))
EventLog.CreateEventSource("MySystemSource", "System", System.Environment.MachineName);
EventLog ev = new EventLog("System", System.Environment.MachineName, "MySystemSource");
/* Writing to system log, in the similar way you can write to other
* logs that you have appropriate permissions to write to
*/
ev.WriteEntry("Warning is written to system Log", EventLogEntryType.Warning, 10001);
MessageBox.Show("Warning is written to System Log");
ev.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void btnWrite_Click(object sender, EventArgs e)
{
WriteLog();
}
private void btnClear_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are You sure want to Delete","Important Question",MessageBoxButtons.YesNo);
try
{
if (Convert.ToString(result) == "Yes")
{
//Create an EventLog instance and pass log name and MachineName where the log resides.
EventLog ev = new EventLog("Security", System.Environment.MachineName);
ev.Clear();
ev.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
http://royalarun.blogspot.in/2012/06/event-viewer-read-write-and-clear-in-c.html