Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# Reading from a file while another program is trying to get exclusive access.

Options
  • 05-08-2013 3:21pm
    #1
    Registered Users Posts: 7,500 ✭✭✭


    I have a FileSystemWater monitoring a directory for changes.
    When the log file is created/modified I am reading this file using:
    FileStream fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
    

    I have a problem with the program which is creating/modifying the log, it is opening the file using the FileShare.None parameter of a FileStream. This means that if it attempts to write while im reading the file it will throw an IOException that the file is in use by another process.

    Unfortunately i cannot modify the 3rd party application to use FileShare.ReadWrite so im trying to figure out a way to read the file without breaking the 3rd party log writer.

    Is it possible?


Comments

  • Registered Users Posts: 2,790 ✭✭✭John_Mc


    Could you copy the file into a temp location and read from that instead? Not the most elegant solution but your hands are tied given that you can't change how the file is written to.


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    Copying the file still requires reading of the file and causes the same problem and i expect the file to get quite large so copying will take longer each time.


  • Registered Users Posts: 2,019 ✭✭✭Colonel Panic


    This highlights one of my problems with how C# uses exceptions for things that aren't exceptional and forces you into using them to control program flow.

    Basically, you need to eat the exceptions in a while loop (add a timeout value too) until you can get read access, then do your thing.

    You still suck at Googling btw :)

    http://stackoverflow.com/questions/10982104/wait-until-file-is-completely-written


  • Registered Users Posts: 2,145 ✭✭✭dazberry


    Have a look at Volume Shadow Copies from .NET. Might be something there to allow you to get a copy of the file while locked.

    D.


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    This highlights one of my problems with how C# uses exceptions for things that aren't exceptional and forces you into using them to control program flow.

    Basically, you need to eat the exceptions in a while loop (add a timeout value too) until you can get read access, then do your thing.

    You still suck at Googling btw :)

    http://stackoverflow.com/questions/10982104/wait-until-file-is-completely-written

    You still suck at reading the OP!!
    The problem isn't with me reading the log, its with the other program writing to the log while im reading it.


  • Advertisement
  • Registered Users Posts: 2,019 ✭✭✭Colonel Panic


    LOL! Touché


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    dazberry wrote: »
    Have a look at Volume Shadow Copies from .NET. Might be something there to allow you to get a copy of the file while locked.

    D.

    Ill have a look at this tonight and see if I can get it working. Thanks.


  • Registered Users Posts: 1,275 ✭✭✭tobsey


    Can you post the name of the third party library? A logger that exclusively locks a text file sounds pretty bad. Are there alternative configurations so that it can log to a different storage format, such as a DB or something?

    Is it possible to suspend the application that the logger is called from? Not the cleanest but your handler for the watcher could perhaps stop the service, copy the file, restart the service and then process the copied file.

    This would really depend on how active the logger is though.


  • Registered Users Posts: 1,275 ✭✭✭tobsey


    This highlights one of my problems with how C# uses exceptions for things that aren't exceptional and forces you into using them to control program flow.

    Basically, you need to eat the exceptions in a while loop (add a timeout value too) until you can get read access, then do your thing.

    You still suck at Googling btw :)

    http://stackoverflow.com/questions/10982104/wait-until-file-is-completely-written
    C# is not throwing the exception. The exception is being thrown in unmanaged code, which is then caught the System.IO.FileStream class and wrapped in a new exception. The underlying Windows API for accessing the file system is the problem.


  • Registered Users Posts: 107 ✭✭McDonnellDean


    It would make more sense to log to memory first and then to a file at regular intervals no? That way you should be reading from memory first. The logger I am working on logs in a similar fashion to how an event aggregator works. It would be up to individual handlers to log to a file or not. Logging to memory kills the need to read files and the only exporting done is via a handler, that way the handler can decide how to do file access, be it batched or per line.

    I am putting the finishing touches on it and will have it on Github in a week or two. I'll post the link here when done.


  • Advertisement
  • Registered Users Posts: 2,781 ✭✭✭amen


    Logging to memory kills the need to read files

    and what happens when you lose the in memory entries ?

    or you have users who need to view the logs but don't have the programming knowledge to access them via a handler ? What about production systems?


  • Registered Users Posts: 107 ✭✭McDonnellDean


    amen wrote: »
    and what happens when you lose the in memory entries ?

    or you have users who need to view the logs but don't have the programming knowledge to access them via a handler ? What about production systems?

    In a production system you would have multiple handlers, once such handler would feed a log view which would break the logs into filterable searchable entries for that view. This is what we do.

    In terms of memory loss, the write handler is designed to be invoked on crash, unhandled exception included. We don't use files as persistence so power cuts wont effect us. Writing to a file is actually just an export to file handler, it cues up log entries, prunes them to look nice in text format and then batches them all to a file at once.

    Interestingly we don't use levels or verbosity anymore, instead we use tags, similar to music, these are alphanumeric and case insensitive, it has vastly improved our facility to log.


  • Registered Users Posts: 107 ✭✭McDonnellDean


    Of course, re reading the question, it will still not solve the issue the user has which is reading in from a third party.


  • Registered Users Posts: 2,781 ✭✭✭amen


    Interestingly we don't use levels or verbosity anymore, instead we use tags, similar to music, these are alphanumeric and case insensitive, it has vastly improved our facility to log

    thats an interesting approach. I might consider the use of Tags.


  • Registered Users Posts: 107 ✭✭McDonnellDean


    amen wrote: »
    thats an interesting approach. I might consider the use of Tags.

    We added it as an after thought to allow people to group related functionality together because we needed to filter the logs somewhat to keep them readable. It turns out that it is the backbone of our logging now and made the viewing of logs by end users really powerful.

    Essentially users can filter a grid by tags and get a very narrow view over a huge log sample, which they are delighted with.


Advertisement