开发者

Read a log file that keeps getting new data from a gameserver

I'm currently creating a reporting bot for an gameserver. The gameserver is Call of Duty 4. This gameserver writes all in-game happenings to an .log file called by default games_mp.log. This file is kept open constantly by the gameserver until you shutdown the gameserver. Now this shouldn't be a problem when using FileShare.ReadWrite so that part is covered. What i want is that i read the new lines added to this logfile at the moment that its being added by the gameserver and place this in IRC.

The IRC part i already got up and running, but the reading the logfile part is not working for me. I've tried several things like the filesystemwatcher but this one doesn't seem to be working. It only reads the file once the server is shutdown. Although the logfile does for an fact get the new data from the as soon something happens on the server. (hope i don't confuse you now :))

So what i want to get done is this: Continuously read the logfile. As soon the gameserver adds an new line to the logfile post this on IRC. Ofcourse ill be format开发者_C百科ting it and check first if it is data i want to be posted on IRC but first i want to get the reading part working.

Here's a part of the code i used when using the Filesystemwatcher:

FileSystemWatcher watcher = new FileSystemWatcher();

    private void watching()
    {
        watcher.Path = @"D:\Games\Call of Duty 4 - Modern Warfare\main";
        watcher.Filter = "games_mp.log";
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        watcher.EnableRaisingEvents = true;
    }

    private void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        ReadNewData();
    }

    private void ReadNewData()
    {
        string line = string.Empty;
        FileStream file = new FileStream(  @"D:\Games\Call of Duty 4 - Modern Warfare\main\games_mp123.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        file.Seek(previousEndPosition, SeekOrigin.Begin);
        TextReader reader = new StreamReader( file);
        while( (line = reader.ReadLine()) != null)
        {
            IRCClass.client.SendChat(line, "#CodTest");
        }
        previousEndPosition = file.Position;
    }

Hopefully someone can help me out.

Pim Dröge


I assume you mean the game server shuts down when you say:

It only reads the file once the server is shutdown

What happens if connection is broken with the game server? Does the FileSystemWatcher.Changed event fire?

It might be the manner in which the server keeps the log file open not closing the filestream and raising the FileSystemWatcher.Changed event.

Have you tried triggering using the NotifyFilter.Size? ie:

watcher.NotifyFilter = NotifyFilters.Size;

by default it uses LastWrite, FileName, DirectoryName. So if for some reason the LastWrite is not being updated but the size is, it would trigger.

To diagnose it further, you could check the file properties using a FileInfo object in a timer to log the properties you are notifying against and verify that they are changing as expected. However, the FileSystemWatcher.Changed event will fire if the NotifyProperty is actually being changed. My guess is a filesystem mechanism on the external server side that does not fully flush to the file until shutdown.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜