Catching page level errors and writing them in log file with auto controlled file size

This is preaty handy for catching page errors and writing them down in log file which is automaticaly deleted if he reaches defined file size.

Imports System.IO

 

Public
Shared Sub PageErr(ByVal ErrorMessage As String, ByVal UserIPAddress As String, _

ByVal URLRefferer As String, ByVal PathToLogFile As String, _

ByVal FileMaxSize As Integer, ByVal FileName As String)

Dim fileExists As Boolean = My.Computer.FileSystem.FileExists(PathToLogFile & FileName)

If fileExists = False Then

Try

My.Computer.FileSystem.WriteAllText(PathToLogFile & FileName, "File created: " & DateTime.Now.ToString, False)

My.Computer.FileSystem.WriteAllText(PathToLogFile & FileName, _

vbCrLf & vbCrLf & "/**************************** " & DateTime.Now.ToString & " ********************************/" & _

vbCrLf & "Error level: Page level" & _

vbCrLf & "Error message: " & ErrorMessage & _

vbCrLf & "URL refferer: " & URLRefferer & _

vbCrLf & "Users' IP: " & UserIPAddress, True)

Catch ex As Exception

End Try

Else

Dim fileSize As Long = My.Computer.FileSystem.GetFileInfo(PathToLogFile & FileName).Length()

If fileSize >= FileMaxSize Then

Try

My.Computer.FileSystem.DeleteFile(PathToLogFile & FileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)

My.Computer.FileSystem.WriteAllText(PathToLogFile & FileName, "File created: " & DateTime.Now.ToString, False)

My.Computer.FileSystem.WriteAllText(PathToLogFile & FileName, _

vbCrLf & vbCrLf & "/**************************** " & DateTime.Now.ToString & " ********************************/" & _

vbCrLf & "Error level: Page level" & _

vbCrLf & "Error message: " & ErrorMessage & _

vbCrLf & "URL refferer: " & URLRefferer & _

vbCrLf & "Users' IP: " & UserIPAddress, True)

Catch ex As Exception

End Try

Else

My.Computer.FileSystem.WriteAllText(PathToLogFile & FileName, _

vbCrLf & vbCrLf & "/**************************** " & DateTime.Now.ToString & " ********************************/" & _

vbCrLf & "Error level: Page level" & _

vbCrLf & "Error message: " & ErrorMessage & _

vbCrLf & "URL refferer: " & URLRefferer & _

vbCrLf & "Users' IP: " & UserIPAddress, True)

End If

End If

End Sub

 

Just pass the aguments and make sure that aspnet user has apropriate read write permissions over the folder where you are going to store the log file

Hope it helps

 

Published Friday, October 10, 2008 3:12 PM by stoian bucovich

Comments

# re: Catching page level errors and writing them in log file with auto controlled file size

Monday, November 17, 2008 5:56 AM by bhavpreet singh

hi..

nice work..

i would be greatful if u provide the same code in asp.net also..

thanks

# re: Catching page level errors and writing them in log file with auto controlled file size

Saturday, February 21, 2009 3:36 AM by stoian bucovich

Hi,

you could paste this code in App_Code folder in new class file and then Import in your Default.aspx.vb and pass the parameters PageErr(_param1, _param2, ..., _paramN) in Page Error event handler.

Hope it helps

Regards,

Stoian Bucovich

Leave a Comment

(required) 
(required) 
(optional)
(required)