How to dynamically display entries made into a growing log file

  • Thread starter Aspiring .NET Programmer
  • Start date
A

Aspiring .NET Programmer

Happy Friday everyone!!!

I am working on a windows service and a C# application and needed some
help with certain functionality. Please read through my issue below.
Thanks!


I have a windows service which writes into a log file periodically
(text file). I want to create a windows form application, which, upon
invocation should continuously display the contents of the log file.
Even the newly made entries into the log file while the windows form
application has started running should be displayed dynamically at run
time.


Is that possible? and how should I go about it.


If such continuous monitoring of a log file cannot be implemented as a
windows form app, then how else could that be implemented?


The main idea is to continuously display to the user the entries that
are being made to the log file by the windows service.


I would be more than happy to provide any further information.


Thanks a lot for any help!!!
BS
 
G

Guest

Hello BS,

To display the contents of the log file you could do a couple of different
things:

1> Use a timer and display the contents every second (or so).
2> Use a FileSystemWatcher - it is designed to listen to the file system
change notifications and raise events when a directory, or file in a
directory, changes.
3> Use some other interprocess synchronization primative - probably overkill
for what you want to do.
 
A

Aspiring .NET Programmer

Hi Brains

Thanks a lot for that information!
Yes. I did discover the 'filesystemwatcher' class earlier. And was
working on that while I posted my query.

My problem while using that is that the following statement:

watcher.Changed += new FileSystemEventHandler(OnChanged);

does pick up events that may happen at compile time. That is if I have
a break point on the above line and put the statement in a finite loop
and start compiling, stop at one of the break point and go change the
log file i am pointing to and then coninue the next iteration of the
loop, it does identify the modification and calls the delegated method.


However, the appropriate output is only displayed once the compilation
has finished. Now, I need an application that continuosly monitors the
logfile and as and when it identifies any modification in it, displays
the log files contents on the windows form.

Basically need to "file changed" event captured at runtime and not
compile time. If I put the above loop in an infinite loop, obviously
the compilation never ends and I just see a blank form. :(

Am I missing something here?

I havent explored the timer option yet. Any sample code that I may look
at?

Will be glad to provide any other info.

Thanks a lot for your time!
Suman
 
G

Guest

Suman,

I'm not sure I understand your application design nor your use of the terms
"compile time" and "run time". Generally speaking, infinite loops and event
driven UI don't go well together.

The way I would monitor a file and display its contents is to use the
FileSystemWatcher object and in the changed event, update the display to
reflect the new contents. If you're in an infinite loop, your application
would never have a chance to update the display with the new contents of the
file.

In any event, you might want to look up on the Code Project at the following
link. The project sounds like just the ticket you're looking for:

http://www.codeproject.com/csharp/mkfilecontentswatcher.asp
 
A

Aspiring .NET Programmer

Hi Brian.
Thanks a lot for your help!!! I used a combination of the timer and the
FileSystemWacher technique to implement my solution. Thanks a lot once
agian. The example helped mee too!..and my apologies for not being able
to explain my second question properly.

Thanks and have a great day!
Suman
 
A

Aspiring .NET Programmer

Hi Brian.
Thanks a lot for your help!!! I used a combination of the timer and the
FileSystemWacher technique to implement my solution. Thanks a lot once
agian. The example helped mee too!..and my apologies for not being able
to explain my second question properly.

Thanks and have a great day!
Suman
 
A

Aspiring .NET Programmer

Hi Brian.
Thanks a lot for your help!!! I used a combination of the timer and the
FileSystemWacher technique to implement my solution. Thanks a lot once
agian. The example helped mee too!..and my apologies for not being able
to explain my second question properly.

Thanks and have a great day!
Suman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top