You're here: Snippet Directory » Microsoft .NET » ASP.NET (68)
Language:

View your event log

Language: English
Programming Language: VB.NET
Published by: Dennis Pallett [not registered]
Last Update: 5/9/2006
Views: 997

Description

Use this code to view your event log.

Code

1 <%@ Import Namespace="System.Diagnostics" %> 2 <%@ Import Namespace="System.Drawing" %> 3 <script language="VB" runat="server"> 4 Sub Page_Load(sender As Object, e As Eventargs) 5 If Not Page.IsPostBack Then 6 DisplayEventLog("System") 7 End If 8 End Sub 9 10 Sub btnSubmit_OnClick(sender As Object, e As eventargs) 11 DisplayEventLog(lstlog.SelectedItem.Value) 12 End Sub 13 14 Sub btnClear_OnClick(sender As Object, e As Eventargs) 15 Dim objEventLog As New EventLog(lstLog.SelectedItem.Value) 16 objEventLog.Clear() 17 End Sub 18 19 Sub DisplayEventlog(strLogName As String) 20 Dim objRow As New TableRow 21 Dim objCell As New TableCell 22 23 objCell.BackColor = Color.Bisque 24 objCell.HorizontalAlign = HorizontalAlign.Center 25 objCell.Text = "Type" 26 objRow.Cells.Add(objCell) 27 28 objCell = New TableCell 29 objCell.BackColor = Color.Bisque 30 objCell.HorizontalAlign = HorizontalAlign.Center 31 objCell.Text = "Date" 32 objRow.Cells.Add(objCell) 33 34 objCell = New TableCell 35 objCell.BackColor = Color.Bisque 36 objCell.HorizontalAlign = HorizontalAlign.Center 37 objCell.Text = "Time" 38 objRow.Cells.Add(objCell) 39 40 objCell = New TableCell 41 objCell.BackColor = Color.Bisque 42 objCell.HorizontalAlign = HorizontalAlign.Center 43 objCell.Text = "Source" 44 objRow.Cells.Add(objCell) 45 46 objCell = New TableCell 47 objCell.BackColor = Color.Bisque 48 objCell.HorizontalAlign = HorizontalAlign.Center 49 objCell.Text = "User" 50 objRow.Cells.Add(objCell) 51 52 objCell = New TableCell 53 objCell.BackColor = Color.Bisque 54 objCell.HorizontalAlign = HorizontalAlign.Center 55 objCell.Text = "Computer" 56 objRow.Cells.Add(objCell) 57 58 tblLog.Rows.Add(objRow) 59 60 Dim objEventLog As EventLog = New EventLog(strLogName) 61 62 Dim objEntry As EventLogEntry 63 64 65 66 For Each objEntry In objEventLog.Entries 67 68 objRow = New TableRow 69 70 objCell = New TableCell 71 72 73 74 75 76 'Callout the error if there is one 77 78 If objEntry.EntryType = EventLogEntryType.Error Then 79 80 objCell.BackColor = Color.Red 81 82 objCell.ForeColor = Color.White 83 84 objCell.Text = "Error" 85 86 ElseIf objEntry.EntryType = EventLogEntryType.Information Then 87 88 objCell.Text = "Information" 89 90 ElseIf objEntry.EntryType = EventLogEntryType.Warning Then 91 92 objCell.BackColor = Color.Yellow 93 94 objCell.Text = "Warning" 95 96 ElseIf objEntry.EntryType = EventLogEntryType.SuccessAudit Then 97 98 objCell.Text = "Success Audit" 99 100 ElseIf objEntry.EntryType = EventLogEntryType.FailureAudit Then 101 102 objCell.ForeColor = Color.Red 103 104 objCell.Text = "Failure Audit" 105 106 End If 107 108 109 110 111 112 113 114 objCell.HorizontalAlign = HorizontalAlign.Center 115 116 objRow.Cells.Add(objCell) 117 118 119 120 121 122 objCell = New TableCell 123 124 objCell.Text = objEntry.TimeGenerated.ToShortDateString() 125 126 objRow.Cells.Add(objCell) 127 128 129 130 131 132 objCell = New TableCell 133 134 objCell.Text = objEntry.TimeGenerated.ToLongTimeString() 135 136 objRow.Cells.Add(objCell) 137 138 139 140 objCell = New TableCell 141 142 objCell.Text = objEntry.Source 143 144 objRow.Cells.Add(objCell) 145 146 147 148 objCell = New TableCell 149 150 If objEntry.Username <> Nothing Then 151 152 objCell.Text = objEntry.UserName 153 154 Else 155 156 objCell.Text = "N/A" 157 158 End If 159 160 objRow.Cells.Add(objCell) 161 162 163 164 objCell = New TableCell 165 166 objCell.Text = objEntry.MachineName 167 168 objRow.Cells.Add(objCell) 169 170 171 172 173 174 175 176 tblLog.Rows.Add(objRow) 177 178 179 180 Next 181 182 183 184 End Sub 185 186 187 188 189 190 </script> 191 192 193 194 195 196 <html> 197 198 199 200 <body> 201 202 203 204 <form runat="server"> 205 206 207 208 <h1>Event Viewer</h1> 209 210 211 212 <asp:listbox runat="server" id="lstLog" Rows="1"> 213 214 215 216 <asp:listitem>Application</asp:listitem> 217 218 <asp:listitem>Security</asp:listitem> 219 220 <asp:listitem Selected="True">System</asp:listitem> 221 222 </asp:listbox> 223 224 225 226 <asp:button runat="server" id="btnSubmit" text="Display Event Log" OnClick="btnSubmit_OnClick" /> 227 228 229 230 <hr> 231 232 233 234 <asp:table runat="server" id="tblLog" CellPadding="5" Cellspacing="0" Width="100%" GridLines="Both" Font-Size="10pt" Font-Name="Verdana" /> 235 236 237 238 <asp:button runat="server" id="btnClear" Text="Clear Event Log" OnClick="btnClear_OnClick" /> 239 240 </form> 241 242 </body> 243 244 </html>

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS