0

You have a c# app.

I have created my own logging file.

When my app is running I would open this log file. Then I would like to see the changes occurring, whilst the app is running and 'recording'.

I have looked around, but all I could find is how to read a file, that is being updated rather than the other way round.

I only have this so far...

    protected void LogSync(string message)
    {
        try
        {
            lock (SynLock)
            {
                using (var sw = new StreamWriter(SyncFilename, true))
                {
                    sw.Write(message + Environment.NewLine);
                    sw.Close();
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
Fred
  • 3,365
  • 4
  • 36
  • 57
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • I think you need to close your file log and open it after you app doing something to show the result . – osman Rahimi Jan 20 '18 at 10:27
  • @osmanRahimi Sorry but that is my question Is it possible to see the update without closing and opening the log file. I know it is possible I just need to know how – Andrew Simpson Jan 20 '18 at 10:29
  • 1
    check this :https://stackoverflow.com/questions/18632/how-to-monitor-a-text-file-in-realtime – osman Rahimi Jan 20 '18 at 10:33
  • @osmanRahimi thanks I know of these tools but I want to do it myself. Hence my question. Thanks – Andrew Simpson Jan 20 '18 at 10:34
  • Do you want to write some text in a text file and read all content or changes of this write action immediately with one run ship? – Fred Jan 20 '18 at 10:35
  • 1
    For the record, your `using (var fs = new FileStream(SyncFilename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))` code is unnecessary. The `StreamWriter` will create the file if it doesn't exist. Also, you don't need to call `Close()` or `Dispose()` on an object that you've wrapped in a `using` statement. The reason you wrap it in `using` is so that it'll dispose itself, even in the event that an exception occurs. – Visual Vincent Jan 20 '18 at 10:50
  • 1
    @VisualVincent thanks for that :) – Andrew Simpson Jan 20 '18 at 10:52
  • @Fred Not sure what u mean here. I start my app. I open my log file. Any changes to it I want to see visually immediatly as it happens without closing and opening the log file. I seen it done before but I cannot remember how. I may have to reference the log file by using a lower level DLL import and use the handle to that file... – Andrew Simpson Jan 20 '18 at 10:55
  • _If_ only you are writing to the log file from within the same app, why can't you just make your `LogSync()` method report the new content that is being added? – Visual Vincent Jan 20 '18 at 10:59
  • It's hard to detect what the changes are without having to read the entire file, so why not just use a `FileSystemWatcher` and re-read all the file's contents when a change occurs? https://stackoverflow.com/q/9757018 – Visual Vincent Jan 20 '18 at 11:02
  • @VisualVincent I am opening the file manually out side of my app. – Andrew Simpson Jan 20 '18 at 11:49
  • Oh, but in what application are you opening it? In Notepad (or similar) or in a custom application of yours? **If the former:** What you are trying to do is not possible. **If the latter:** Refer to my previous comment about using a `FileSystemWatcher`. – Visual Vincent Jan 20 '18 at 13:30
  • in notepad. And it is possible. I have seen it in operation – Andrew Simpson Jan 20 '18 at 15:01
  • @PeterDuniho I was not looking for a tool I was looking for a c# solution as my question and subsequent comments illustrated. *thanks* – Andrew Simpson Jan 21 '18 at 09:00
  • It couldn't possibly have been Notepad on its own that you've seen, unless there were another app running in the background. Windows's Notepad doesn't update itself automatically. You can do it via your application using some sort of automation, but it'll be very complicated and would require you to P/Invoke [**`SendMessage()`**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx) and other functions to perform the automation in question. You also have to make sure that you've found the **correct** Notepad instance before you update the process's text. – Visual Vincent Jan 22 '18 at 16:51
  • @VisualVincent Yes, i imagined i would have to use pInvoke and use sendMessage, Finding the correct handle for Notepad is not that difficult. – Andrew Simpson Jan 22 '18 at 18:03
  • It is if the user has multiple instances opened... – Visual Vincent Jan 23 '18 at 08:23
  • @VisualVincent hi. Yes, but as long as i think of something unique for the filename then all should be good – Andrew Simpson Jan 23 '18 at 08:24

1 Answers1

0

You can use FileSystemWatcher to monitor a path. please read this post

Using FileSystemWatcher to monitor a directory

FileSystemWatcher: Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

Fred
  • 3,365
  • 4
  • 36
  • 57