处理大文件

时间:2012-06-24 20:28:31

标签: .net f#

我有一个大型日志文件,我需要在Windows 8上使用F#进行解析。服务器一直在运行,通过添加新行写入日志文件的末尾。每分钟都会检查日志文件是否有变化。我不想重新整理整个文件,而是从最后读取并将新添加的行与前一个解析的结果相结合。更好的是,我希望能够在文件发生变化时注册回调。这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以通过文件流Seek()执行此操作。像这样:

open System.IO

let WatchLog directory file =
    // Keep track of last read position
    let lastRead = ref 0L

    // Function that's called whenever the file changes
    let processLog (e : FileSystemEventArgs) =
        // Open file, and seek to the last read location
        use fs = new FileStream(Path.Combine(directory, file), FileMode.Open, FileAccess.Read)
        fs.Seek(!lastRead, SeekOrigin.Begin) |> ignore

        // Read the rest of the file
        use sr = new StreamReader(fs)
        printfn "Reading log: %A" (sr.ReadToEnd())

        // Store the length so that we know how much to seek over next time
        lastRead := fs.Length
        ()

    // Create a FS watched to be notified when a file changes
    let fs = new FileSystemWatcher(directory, file)
    fs.Changed.Add(processLog)
    fs.EnableRaisingEvents <- true

WatchLog "M:\Coding\" "TestLog.txt"

但是,出于某种原因,在我的机器上,我得到“进程无法访问该文件,因为它正被另一个proc使用”错误,我无法追查。 use语句应该在超出范围时进行处理,甚至对Close的显式调用也无法解决:/