| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Complete example using custom errors and write errors to the Event log
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Diagnostics" %> <script language="VB" runat=server> Public Sub Application_OnError(Sender as Object, E as EventArgs) 'Captures the error and converts to a string dim LastError as Exception = Server.GetLastError() Dim ErrMessage as String = LastError.toString() Dim LogName As String = "MyLog" Dim Message As String = "Url " & Request.Path & " Error: " & ErrMessage ' Create Event Log if It Doesn't Exist If (Not EventLog.SourceExists(LogName)) Then EventLog.CreateEventSource(LogName, LogName) End if Dim Log as New EventLog Log.Source = LogName 'These are the five options that will display a different icon. 'The numbers are just to show the order. These aren't required Log.WriteEntry(Message, EventLogEntryType.Information, 1) ' Log.WriteEntry(Message, EventLogEntryType.Error, 2) ' Log.WriteEntry(Message, EventLogEntryType.Warning, 3) ' Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4) ' Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5) End Sub </script> [bold]Default.aspx page[/bold] <% @Language="VB" %> <script language="VB" runat=server> Sub Page_Load(Sender As Object, E As EventArgs) If IsPostBack Then 'Declare all variables dim x as integer dim y as integer dim z as integer 'set x and y to values to be divided by zero x = 1 y = 0 'perform the division by zero to raise the error z = x/y End Sub </script> <html> <head> </head> <body> <form method="post" action="eventlog.aspx" name="form1" id="number"> <asp:Button id="abutton" type="submit" text="Click Me to generate an error" runat="server" /> </form> </body> </html> [bold]Customerrorpage.aspx[/bold] <html> <head></head> <body> <h1>custom error page</h1> </body> </html> [bold]404page.aspx --Capture all 404(Not Found pages)[/bold] <html> <head></head> <body> <h1>404 error page</h1> </body> </html> [bold]403page.aspx --Capture all 403(Restricted pages)[/bold] <html> <head></head> <body> <h1>403 error page</h1> </body> </html> Read Complete Article |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Complete example using custom errors and write errors to the Event log |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|