Following is the sample code to write to the windows event log and display the contents of user selected windows event log on ASP.NET page.
Write to the windows event log:
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SQL” %>
<%@ Import Namespace=”System.Diagnostics” %>
<script language=”c#” runat=”server”>
Private Sub Page_Load(source As [Object], e As EventArgs)
' Perform some type of illegal operation
Try
Dim objConn As SQLConnection
objConn = New SQLConnection("server=localhost;uid=foo;pwd=bar;database=pubs")
' ...
objConn.Open()
Catch eError As Exception
RecordError(eError, EventLogEntryType.[Error])
End Try
End Sub
Private Sub RecordError(ByVal eError As Exception, ByVal enumType As EventLogEntryType)
Const strSource As [String] = "ASP.NET", strLogName As [String] = "System"
' Add the strMessage entry to the ASPX event log
Dim objLog As New EventLog(strLogName)
objLog.Source = strSource
objLog.WriteEntry(eError.Message, enumType)
End Sub
</script>
Read the content of the windows event log:
<%@ Import Namespace=”System.Diagnostics” %>
<%@ Import Namespace=”System.Drawing” %>
<script language=”VB” runat=”server”>
Sub Page_Load(ByVal source As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
DisplayEventLog("System")
End If
End Sub
Sub btnSubmit_OnClick(ByVal source As Object, ByVal e As EventArgs)
DisplayEventLog(lstLog.SelectedItem.Value)
End Sub
Sub btnClear_OnClick(ByVal source As Object, ByVal e As EventArgs)
'Clear all of the event log entries
Dim objEventLog As New EventLog(lstLog.SelectedItem.Value)
objEventLog.Clear()
End Sub
Sub DisplayEventLog(ByVal strLogName As String)
Dim objRow As New TableRow
Dim objCell As New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = "Type"
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = "Date"
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = "Time"
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = "Source"
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = "User"
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = "Computer"
objRow.Cells.Add(objCell)
tblLog.Rows.Add(objRow)
Dim objEventLog As EventLog = New EventLog(strLogName)
Dim objEntry As EventLogEntry
For Each objEntry In objEventLog.Entries
objRow = New TableRow
objCell = New TableCell
'Determine the type of error
If objEntry.EntryType = EventLogEntryType.Error Then
objCell.BackColor = Color.Red
objCell.ForeColor = Color.White
objCell.Text = "Error"
ElseIf objEntry.EntryType = EventLogEntryType.Information Then
objCell.Text = "Information"
ElseIf objEntry.EntryType = EventLogEntryType.Warning Then
objCell.BackColor = Color.Yellow
objCell.Text = "Warning"
ElseIf objEntry.EntryType = EventLogEntryType.SuccessAudit Then
objCell.Text = "Success Audit"
ElseIf objEntry.EntryType = EventLogEntryType.FailureAudit Then
objCell.ForeColor = Color.Red
objCell.Text = "Failure Audit"
End If
objCell.HorizontalAlign = HorizontalAlign.Center
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.TimeGenerated.ToShortDateString()
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.TimeGenerated.ToLongTimeString()
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.Source
objRow.Cells.Add(objCell)
objCell = New TableCell
If objEntry.UserName <> Nothing Then
objCell.Text = objEntry.UserName
Else
objCell.Text = "N/A"
End If
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.MachineName
objRow.Cells.Add(objCell)
tblLog.Rows.Add(objRow)
Next
End Sub
</script>
<html>
<body>
<form runat=”server”>
<h1>Event Log Viewer</h1>
<asplistbox runat=”server” id=”lstLog” Rows=”1”>
<asplistitem>Application</asplistitem>
<asplistitem>Security</asplistitem>
<asplistitem Selected=”True”>System</asplistitem>
</asplistbox>
<aspbutton runat=”server” id=”btnSubmit” Text=”Display Event Log” OnClick=”btnSubmit_OnClick” />
</hr>
<asptable runat=”server” id=”tblLog” CellPadding=”5” CellSpacing=”0” GridLines=”Both” Font-Size=”10pt” Font-Name=”Verdana” />
</hr>
<aspbutton runat=”server” id=”btnClear” Text=”Clear Event Log” OnClick=”btnClear_OnClick” />
</form>
</body>
</html>
User selects event log from provided dropdownlist and clicks display event log button; event log contents is displayed on ASP.NET page.
