I got solution with couple of projects including 3 windows services projects and one logger project which is shared class. Due to all services are using same logger project class i need a lock to avoid "process cannot access file because of another process..". In this situation 3 separated services writing to this logger. I made mutex and have two question for you.
1) Is this use of mutex correct and will prevent other service to write but wait till other finish his job?
2) In my case instead of putting: "Global\MyProjectName_823742384234" - can i put: "Global\" + Guid.NewGuid.ToString()" ?. Changing to dynamic guid does my 3 windows services will be still synchronized in case of logger class? Should i do like i have or with newguid?
Here's my logger class:
Imports System.IO
Imports System.Threading
Public NotInheritable Class Logger
Private Shared ReadOnly _locker As New Mutex(False, "MyProjectName_823742384234")
Public Shared Sub LogIt(ByVal engine As String, ByVal msg As String, ByVal logMessage As String, ByVal Path As String, ByVal IsDebug As Boolean)
_locker.WaitOne()
Try
If File.Exists(Path) Then
If IsDebug Then
Debug.Print(DateTime.Now & "> " & "| " & engine & " | " & msg & " | " & logMessage)
' Debug.Print(message)
Else
Using w As TextWriter = File.AppendText(Path)
'w.WriteLine(message)
w.WriteLine(DateTime.Now & "> " & "| " & engine & " | " & msg & " | " & logMessage)
w.Flush()
End Using
End If
Else
If IsDebug Then
' Debug.Print(message)
Debug.Print(DateTime.Now & "> " & "| " & engine & " | " & msg & " | " & logMessage)
Else
Using w As TextWriter = File.CreateText(Path)
' w.WriteLine(message)
w.WriteLine(DateTime.Now & "> " & "| " & engine & " | " & msg & " | " & logMessage)
w.Flush()
End Using
End If
End If
Finally
_locker.ReleaseMutex()
End Try
End Sub
End Class
No comments:
Post a Comment