blondebier
Programmer
Hi Guys,
I have a vb.net WebService that has recently started getting very busy. This is good, but we now realise that the activity/error logging class that we have is not suitable as it is not thread safe.
This is an example of my Activity Log.
06/11/2007 14:38:55 LIVE: Start CheckAddress WebMethod ****************************************************************
06/11/2007 14:38:55 LIVE: XML Recieved to CheckAddress
06/11/2007 14:38:55 LIVE: <?xml version="1.0" encoding="utf-8"?><CheckAddress><ClientID>username</ClientID><ClientPWD>secret</ClientPWD><Address><HouseNumber>1</HouseNumber><Street>Toy Street</Street><District /><Town>Toy Town</Town><County>My County</County><Postcode>PC1 1CD</Postcode><AtAddressFrom>01/01/2003</AtAddressFrom><AtAddressTo>01/01/2006</AtAddressTo><AddressType>U</AddressType></Address></CheckAddress>
06/11/2007 14:38:56 LIVE: UserID: 1
06/11/2007 14:38:57 LIVE: Validating the address...
06/11/2007 14:38:57 LIVE: Validation complete. Final XML response...
06/11/2007 14:38:57 LIVE: <?xml version="1.0" encoding="UTF-8"?><Result>AddressOK</Result>
06/11/2007 14:38:57 LIVE: End CheckAddress WebMethod ****************************************************************
This is fine when requests come in one at a time, but if 2 or more requests are received by the web service at the same time or very close together, the Activity log entries get mixed up and I can't see what is going on.
I have a module in my project as follows:
Module _Module
Public Sub dBug(ByVal Narrative As String)
Dim log As New ErrorLog
Dim debug As Boolean
Dim environment As String = ""
Try
debug = CType(ConfigurationManager.AppSettings("Debug"), Boolean)
environment = ConfigurationManager.AppSettings("Environment")
' Only write to the error log if debugging is enabled
If debug Then
If Narrative = "" Then
log.WriteError("X")
Else
log.WriteError("Log " & Now & " " & environment & ": " & Narrative)
End If
End If
Catch sii As System.IO.IOException
Try
log.WriteError("Log " & Now & " " & environment & ": " & Narrative)
Catch ex As Exception
' Do Nothing
End Try
Catch ex As Exception
' Do Nothing
End Try
End Sub
End Module
I can then reference the dbug() function wherever I need to in my WebService for writing debug statements.
My ErrorLog class is as follows:
Public Class ErrorLog
Public Sub WriteError(ByVal ErrorMessage As String, Optional ByVal FilePath As String = "C:\")
Dim LogPath, environment As String
LogPath = ConfigurationManager.AppSettings("DebugLogLocation")
environment = ConfigurationManager.AppSettings("Environment")
If LogPath = "" Then LogPath = FilePath
LogPath = LogPath & environment & " Log - " & Format(Date.Today, "d MMM yyyy") & ".log"
If File.Exists(LogPath) = True OrElse CheckFilePath(LogPath) Then
Dim sr As StreamWriter = File.AppendText(LogPath)
sr.WriteLine(ErrorMessage)
sr.Close()
End If
End Sub
Private Function CheckFilePath(ByVal FilePath As String) As Boolean
Dim strChar As String
Try
If FilePath > "" Then
strChar = ""
If Not Directory.GetParent(FilePath).Exists Then
Do While Directory.GetParent(FilePath).Exists = False
strChar = Directory.GetParent(FilePath).ToString
Do While Not Directory.GetParent(strChar).Exists And strChar > ""
strChar = Directory.GetParent(strChar).ToString
Loop
Directory.CreateDirectory(strChar)
Loop
End If
End If
CheckFilePath = True
Catch Ex As Exception
CheckFilePath = False
End Try
End Function
End Class
Any idea how I could modify this so that it is threadsafe so that all the requests are written to the log in order?
I guess that instantiating a new instance of ErrorLog every time dBug is called is probably something that definitely needs to change...
Any pearls of wisdom or working examples would be most appreciated.
Cheers.
I have a vb.net WebService that has recently started getting very busy. This is good, but we now realise that the activity/error logging class that we have is not suitable as it is not thread safe.
This is an example of my Activity Log.
06/11/2007 14:38:55 LIVE: Start CheckAddress WebMethod ****************************************************************
06/11/2007 14:38:55 LIVE: XML Recieved to CheckAddress
06/11/2007 14:38:55 LIVE: <?xml version="1.0" encoding="utf-8"?><CheckAddress><ClientID>username</ClientID><ClientPWD>secret</ClientPWD><Address><HouseNumber>1</HouseNumber><Street>Toy Street</Street><District /><Town>Toy Town</Town><County>My County</County><Postcode>PC1 1CD</Postcode><AtAddressFrom>01/01/2003</AtAddressFrom><AtAddressTo>01/01/2006</AtAddressTo><AddressType>U</AddressType></Address></CheckAddress>
06/11/2007 14:38:56 LIVE: UserID: 1
06/11/2007 14:38:57 LIVE: Validating the address...
06/11/2007 14:38:57 LIVE: Validation complete. Final XML response...
06/11/2007 14:38:57 LIVE: <?xml version="1.0" encoding="UTF-8"?><Result>AddressOK</Result>
06/11/2007 14:38:57 LIVE: End CheckAddress WebMethod ****************************************************************
This is fine when requests come in one at a time, but if 2 or more requests are received by the web service at the same time or very close together, the Activity log entries get mixed up and I can't see what is going on.
I have a module in my project as follows:
Module _Module
Public Sub dBug(ByVal Narrative As String)
Dim log As New ErrorLog
Dim debug As Boolean
Dim environment As String = ""
Try
debug = CType(ConfigurationManager.AppSettings("Debug"), Boolean)
environment = ConfigurationManager.AppSettings("Environment")
' Only write to the error log if debugging is enabled
If debug Then
If Narrative = "" Then
log.WriteError("X")
Else
log.WriteError("Log " & Now & " " & environment & ": " & Narrative)
End If
End If
Catch sii As System.IO.IOException
Try
log.WriteError("Log " & Now & " " & environment & ": " & Narrative)
Catch ex As Exception
' Do Nothing
End Try
Catch ex As Exception
' Do Nothing
End Try
End Sub
End Module
I can then reference the dbug() function wherever I need to in my WebService for writing debug statements.
My ErrorLog class is as follows:
Public Class ErrorLog
Public Sub WriteError(ByVal ErrorMessage As String, Optional ByVal FilePath As String = "C:\")
Dim LogPath, environment As String
LogPath = ConfigurationManager.AppSettings("DebugLogLocation")
environment = ConfigurationManager.AppSettings("Environment")
If LogPath = "" Then LogPath = FilePath
LogPath = LogPath & environment & " Log - " & Format(Date.Today, "d MMM yyyy") & ".log"
If File.Exists(LogPath) = True OrElse CheckFilePath(LogPath) Then
Dim sr As StreamWriter = File.AppendText(LogPath)
sr.WriteLine(ErrorMessage)
sr.Close()
End If
End Sub
Private Function CheckFilePath(ByVal FilePath As String) As Boolean
Dim strChar As String
Try
If FilePath > "" Then
strChar = ""
If Not Directory.GetParent(FilePath).Exists Then
Do While Directory.GetParent(FilePath).Exists = False
strChar = Directory.GetParent(FilePath).ToString
Do While Not Directory.GetParent(strChar).Exists And strChar > ""
strChar = Directory.GetParent(strChar).ToString
Loop
Directory.CreateDirectory(strChar)
Loop
End If
End If
CheckFilePath = True
Catch Ex As Exception
CheckFilePath = False
End Try
End Function
End Class
Any idea how I could modify this so that it is threadsafe so that all the requests are written to the log in order?
I guess that instantiating a new instance of ErrorLog every time dBug is called is probably something that definitely needs to change...
Any pearls of wisdom or working examples would be most appreciated.
Cheers.