Thursday, February 5, 2009

code for Global.asax

Global.asax Page - This uses the Application_OnError event to capture stuff if an error happens
C#
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception LastError = Server.GetLastError();
string ErrMessage = LastError.ToString();
string LogName = "MyLog";
string Message = "Url " + Request.Path + " Error: " + ErrMessage;
// Create Event Log if It Doesn't Exist
if (!EventLog.SourceExists(LogName))
{
EventLog.CreateEventSource(LogName, LogName);
}

EventLog Log=new EventLog() ;
Log.Source = LogName;
//These are the five options that will display a different icon
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);
}


VB.NET
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

Default.aspx page

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

write script

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"

No comments:

Post a Comment