|
|
|
Click here to copy the following block | ' This class extends the base Exception class with a static method (callable ' also without creating and throwing an instance of this exception class) to ' log the contents of the exception to a file.
' Notes: ' A custom key named ErrorLogFile must be added to the <appSettings> section, ' within the <configuration> section, to specify the path of the log file, ' as follows: ' <appSettings> ' <add key="ErrorLogFile" value="~/ErrorLog.txt" /> ' </appSettings>
' Usage: Throw New WebModules.AppException("This error message will be saved to ' file")
Imports System Imports System.Web Imports System.Diagnostics
Namespace WebModules
' Default exception to be thrown by the website, it will automatically ' log the contents of the exception to a file. ' --- Public Class AppException Inherits System.Exception
' Constructors '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Public Sub New() LogError("An unexpected error occurred.") End Sub
Public Sub New(ByVal message As String) LogError(message) End Sub
Public Sub New( ByVal message As String, ByVal innerException As Exception)
LogError(message)
If Not (innerException Is Nothing) Then LogError(innerException.Message.ToString) End If
End Sub
' Shared Methods '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Public Shared Sub LogError(ByVal message As String)
' Get the current HTTPContext Dim context As HttpContext = HttpContext.Current
' Get location of ErrorLogFile from Web.config file Dim filePath As String = context.Server.MapPath( CStr _ (System.Configuration.ConfigurationSettings.AppSettings( _ "ErrorLogFile")))
' Calculate GMT offset Dim gmtOffset As Integer = DateTime.Compare(DateTime.Now, DateTime.UtcNow)
Dim gmtPrefix As String If gmtOffset > 0 Then gmtPrefix = "+" Else gmtPrefix = "" End If
' Create DateTime string Dim errorDateTime As String = DateTime.Now.Year.ToString & "." & _ DateTime.Now.Month.ToString & "." & DateTime.Now.Day.ToString & " @ " _ & DateTime.Now.Hour.ToString & ":" & DateTime.Now.Minute.ToString & _ ":" & DateTime.Now.Second.ToString & " (GMT " & gmtPrefix & _ gmtOffset.ToString & ")"
' Write message to error file Try Dim sw As New System.IO.StreamWriter(filePath, True) sw.WriteLine("## " & errorDateTime & " ## " & message & " ##") 'sw.WriteLine(message) 'sw.WriteLine() sw.Close() Catch ' If error writing to file, simply continue End Try
End Sub
End Class
End Namespace |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|