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

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as