vb.net - System.IO.IOException: The process cannot access the file -


hi programmers colleagues! hope know how solve issue. time time not error in subject when writing log file. got 3 windows services in solution. using same file logs. unfortunetly time time error. code logger showed below. used synlock , thought enough not. can me should change avoid issue please?:

public notinheritable class logger      private shared readonly _locker new object()      public shared sub logit(byval engine string, byval msg string, byval logmessage string, byval path string, byval isdebug boolean)               if file.exists(path)                 if isdebug                     debug.print(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                     ' debug.print(message)                 else                     synclock _locker                           using w textwriter = file.appendtext(path)                              'w.writeline(message)                             w.writeline(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                             w.flush()                         end using                     end synclock                 end if             else                 if isdebug                     '  debug.print(message)                     debug.print(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                 else                     synclock _locker                         using w textwriter = file.createtext(path)                             '   w.writeline(message)                             w.writeline(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                             w.flush()                         end using                     end synclock                 end if             end if         end sub  end class 

updated:

imports system.io imports system.threading  public notinheritable class logger      private shared readonly _locker new mutex(false, "global\anyhardtoguessname")     public shared sub logit(byval engine string, byval msg string, byval logmessage string, byval path string, byval isdebug boolean)           _locker.waitone()          try             if file.exists(path)                 if isdebug                     debug.print(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                     ' debug.print(message)                 else                         using w textwriter = file.appendtext(path)                              'w.writeline(message)                             w.writeline(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                             w.flush()                         end using                 end if             else                 if isdebug                     '  debug.print(message)                     debug.print(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                 else                         using w textwriter = file.createtext(path)                             '   w.writeline(message)                             w.writeline(datetime.now & "> " & "| " & engine & " | " & msg & " | " & logmessage)                             w.flush()                         end using                 end if             end if                      _locker.releasemutex()         end try      end sub  end class 

i got 3 windows services in solution

that produces 3 separate exes, not share _locker object. visibility of .net objects restricted process in created. nothing prevents these programs trying open log file @ same time. or trying create log file @ same time, have bug in file.exists usage, must inside synclock statement prevent race when 2 threads both check file existence @ same time.

by far best solution have each exe use own log file _locker object enough. if sharing absolutely essential must use named mutex allow these programs share same synchronization object across process boundaries. in other words:

 private shared readonly _locker new mutex(false, "global\anyhardtoguessname")       public shared sub logit(...)         _locker.waitone()         try             ''...                     _locker.releasemutex()         end try      end sub 

add imports system.threading @ top of source file.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -