devxlogo

AppException – An extended Exception class that logs the errors to file

AppException – An extended Exception class that logs the errors to file

' 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  section,'  within the  section, to specify the path of the log file,'  as follows:'    '       '    ' Usage: Throw New WebModules.AppException("This error message will be saved to ' file")Imports SystemImports System.WebImports System.DiagnosticsNamespace 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 ClassEnd Namespace' This code is taken from Marco Bellinaso's and Kevin Hoffman's "ASP.NET ' Website Programming - VB.NET edition" (Wrox Press). You can read two entire ' sample chapters of the C# edition from our Book Bank:' Chapter 4: Mantaining the site: http://www.vb2themax.com/Htmldoc.asp?File=/' Books/AspnetWebsite/AspNetWebSite_04.htm' Chapter 11: Deploying the Site: http://www.vb2themax.com/HtmlDoc.asp?Table=' Books&ID=7800

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist