Imports System.IO
Imports System.Text
Imports System.Windows.Forms

<CLSCompliant(True)> _
Public Class ErrorLogger
  Inherits System.Web.UI.Page
  Private Shared m_fs As FileStream
  Private Shared m_wLog As System.IO.StreamWriter
  Public Sub New()
    'default constructor
  End Sub
  Public Sub New(ByVal FolderName As String, ByVal FileName As String)
    If (FolderName = "") Then
      FolderName = "Log\"
    End If
    FolderName = IIf(Right(FolderName, 1) = "\", FolderName, FolderName & "\")
    If Not System.IO.Directory.Exists(Server.MapPath(FolderName)) Then
      System.IO.Directory.CreateDirectory(Server.MapPath(FolderName))
    End If
    m_fs = New FileStream(Server.MapPath(FolderName & FileName), FileMode.OpenOrCreate, FileAccess.ReadWrite)
    m_wLog = New System.IO.StreamWriter(m_fs)
  End Sub
  Public Shared Function write2Log(ByVal sMsg As String, Optional ByVal bAddTime As Boolean = False, Optional ByVal nTime As Long = -1, _
    Optional ByVal bTimeBeforeMsg As Boolean = True, Optional ByVal UID As String = "") As Integer
    '*****************************************************************
    'Copyright © 2005 ESRI. All Rights Reserved.
    '*****************************************************************
    'Description: write sMsg to the project logfile.
    'History: zye @12/03/2004 2:56:19 PM
    '*****************************************************************
    Dim i As Integer
    Try
      If Not m_wLog Is Nothing Then
        If (UID <> "") Then
          sMsg = "UID=" & UID & ", " & sMsg
        End If
        If bAddTime = True Then
          If bTimeBeforeMsg = False Then
            sMsg = sMsg & " @" & Date.Now
          Else
            sMsg = Date.Now & " " & sMsg
          End If
        End If
        If nTime > 0 Then
          Dim dDate As DateTime = DateTime.Now '..nTime as ticks.
          sMsg = sMsg & " ds=" & ((dDate.Ticks - nTime) / TimeSpan.TicksPerSecond) & "s"
        End If
        m_wLog.WriteLine(sMsg)
        m_wLog.Flush()
        i = 0
      End If
    Catch ex As Exception
      i = -1
    End Try
    Return i
  End Function
  '*************************************************************
  'NAME:          WriteToErrorLog
  'PURPOSE:       Open or create an error log and submit error message
  'PARAMETERS:    msg - message to be written to error file
  '               stkTrace - stack trace from error message
  '               title - title of the error file entry
  'RETURNS:       Nothing
  '*************************************************************
    Public Sub WriteToErrorLog(ByVal msg As String, ByVal stkTrace As String, ByVal title As String)
    'check and make the directory if necessary; this is set to look in the application
    'folder, you may wish to place the error log in another location depending upon the
    'the user's role and write access to different areas of the file system
    If Not System.IO.Directory.Exists(Server.MapPath("Errors\")) Then
      System.IO.Directory.CreateDirectory(Server.MapPath("Errors\"))
    End If

    'check the file
    Dim fs As FileStream = New FileStream(Server.MapPath("Errors\errlog.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite)
    Dim s As StreamWriter = New StreamWriter(fs)
    s.Close()
    fs.Close()

    'log it
    Dim fs1 As FileStream = New FileStream(Server.MapPath("Errors\errlog.txt"), FileMode.Append, FileAccess.Write)
    Dim s1 As StreamWriter = New StreamWriter(fs1)
    s1.Write("Title: " & title & vbCrLf)
    s1.Write("Message: " & msg & vbCrLf)
    s1.Write("StackTrace: " & stkTrace & vbCrLf)
    s1.Write("Date/Time: " & DateTime.Now.ToString() & vbCrLf)
    s1.Write("===========================================================================================" & vbCrLf)
    s1.Close()
    fs1.Close()

  End Sub

  Protected Overrides Sub Finalize()
    MyBase.Finalize()
    If Not m_fs Is Nothing Then
      write2Log(System.Reflection.Assembly.GetExecutingAssembly.Location() & " Application stopped ", True)
      'm_wLog.Close()
      'm_fs.Close()
    End If
  End Sub
End Class
