Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a log file that I have to trace in real time with signalr. I'm using the mapping to remote method and FileSystemEventHandler approach as follow : This is the method that watches the log file.

public void Progress_Log(SchemaUpdate imp)
{
    // Preparing remote mapping
    string strCmdText;
    strCmdText = "/C net use /delete k: /y";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);
    Thread.Sleep(500);
    strCmdText = @"/C net use K: \\xx\e$\test\dumps\Create /user:xx xxxx";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);
    var watch = new FileSystemWatcher
    {
        Path = @"\\TN1ORC21\e$\test\dumps\Create",
        Filter = "output.txt"
    };
    //watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite; //more options
    watch.Changed += new FileSystemEventHandler(OnChanged);
    watch.EnableRaisingEvents = true;
}


As soon as a new line is added to the file the onChanged method is called :

public void OnChanged(object source, FileSystemEventArgs e)
{
    using StreamReader r = new StreamReader(System.IO.File.Open(@"k:\" + e.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
    string line;
    while (!r.EndOfStream)
    {
        line = r.ReadLine();
        if (r.Peek() == -1)
        {
            HubContext.Clients.All.SendAsync("ReceiveMessage", "Log", line);
            // THIS BASICALLY SENDS THE LINE TO THE HUB THEN TO THE CLIENT (BROWSER)
        }
    }
}


What I have tried:

The problem is that it doesn't print every new added lines and skips alot of lines that I need to see and for example for a file of 200 lines I'll get like 7 lines max. I figured my problem is in the while loop of the onChanged method but I can't figure out how to fix this.
Posted
Updated 16-May-20 15:34pm

1 solution

Well .. I wouldn't be using the FileSystemEventHandler OnChanged event to attempt to get every line/group of lines added - there's too much buffering etc going on, and have a look here FileSystemWatcher.Changed Event (System.IO) | Microsoft Docs[^] about the number of events that can be raised - JSOP a programmer here in CodeProject demonstrated this nicely in code, the first of his cases in this article FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]

Here's what I have done in the past
1) I used the FileSystemEventHandler OnChanged event to say when an 'external process' started writing to the log file.
2) I then had the equivalent of a 'tail' written in c# that I used to scan the lines of the log-file
3) The tail (and process) would stop when the required data was found or the external process indicated in the log file it had finished (there are other ways of handling this)

There was a C# Tail implementation I found that I used as the model for mine ...
 
Share this answer
 
Comments
dreevo 18-May-20 7:28am    
Can you please help me implement that ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900