C
cj2
Since a web service is basically multi-threaded. I was wondering how to
allow it to write to a file and not have all the threads tramping on
eachother.
I did it this way in a multi-threaded windows app I wrote recently.
Would this apply to a web service as well. It seems to work but it's
hard to say if problems will show under heavy load. Since this is for
logging when the web service can't write to it's primary log file (sql
server) it will only be used in a situation where sql is down. I hope
this code is never used but if it needs to be it must work.
from within the web service:
try
I do my sql insert
catch ex as exception
MyStringLogger.Write("\\fileserver\I\NewValLogs\" & Format(Now(),
"yyMMdd") & ".err", Now() & "|" & ResponseReasonCode & "|Save results
exception: " & ex.Message)
end try
Public Class MyStringLogger
Private Shared m_loglock As New Object
Public Shared Sub Write(ByVal fileName As String, ByVal strToWrite
As String)
SyncLock (m_loglock)
Try
Dim sw As New System.IO.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
sw.Close()
sw.Dispose()
Catch ex As Exception
End Try
End SyncLock
End Sub
end class
allow it to write to a file and not have all the threads tramping on
eachother.
I did it this way in a multi-threaded windows app I wrote recently.
Would this apply to a web service as well. It seems to work but it's
hard to say if problems will show under heavy load. Since this is for
logging when the web service can't write to it's primary log file (sql
server) it will only be used in a situation where sql is down. I hope
this code is never used but if it needs to be it must work.
from within the web service:
try
I do my sql insert
catch ex as exception
MyStringLogger.Write("\\fileserver\I\NewValLogs\" & Format(Now(),
"yyMMdd") & ".err", Now() & "|" & ResponseReasonCode & "|Save results
exception: " & ex.Message)
end try
Public Class MyStringLogger
Private Shared m_loglock As New Object
Public Shared Sub Write(ByVal fileName As String, ByVal strToWrite
As String)
SyncLock (m_loglock)
Try
Dim sw As New System.IO.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
sw.Close()
sw.Dispose()
Catch ex As Exception
End Try
End SyncLock
End Sub
end class