2

I need a way of extreme fast logging (frame info about highspeed camera). Its just a few numbers that I need to log, and a simple file.log. Eventlogging is to slow for this.

So then I thought, well just create a file stream so i can lock the file for my app. and append to it.

Normally I would use a simple line such as

Filestream fs = new FileStream(@"D:\Log.csv", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);

inside a method.

However since the frames by the camera driver are executed each in a new thread I got a problem here. As I dont want to re-open and close the file each time the log file is written to. (open and close is slow).

I'd like to open the log file once, at the start of my program, and the threads should only perform a write to it, not closing and opening it again and again.

How to achieve this, since this doesn't work :

using System.IO;
FileStream fs = new FileStream(@"D:\Log.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);

    static void Main(string[] args)
    {
         // doing it in main doesn't work either.
         fs = new FileStream(@"D:\Log.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
       //...
       //.. init camera and start camera lots of code follows but is not related to the question.
     }

     Camera_Thread.FrameArrived (FrameArrivedEventArgs e)
    {   
       byte[] n = MyFilterFunction(e.frame);         
       fs.WriteByte(MyArrayToString(n));
    }
9
  • "doesn't work" is pretty unspecific. Please clarify what the actual problem is. Apr 22, 2016 at 10:25
  • Why not use a TextWriterTraceListener
    – qxg
    Apr 22, 2016 at 10:25
  • @LasseV.Karlsen, i've tried several combination of declaring this file the compiler wont accept on how i try to declare it, ea the fs filestream variable is not recognized in camera_thread.FrameArived as fs isnt a global variable
    – Peter
    Apr 22, 2016 at 10:30
  • 1
    I would use some logging library, like nlog for this
    – kfazi
    Apr 22, 2016 at 10:31
  • 1
    This has been solved in logging frameworks like NLog and Log4Net. Both support writing to disk asynchronously and support logging from multiple threads to a single file. Apr 22, 2016 at 10:32

1 Answer 1

1

There's a number of ways, but most if not all involve queueing, especially in a multithreaded environment.

You could use MSMQ to queue your logs for processing, you can also use a separate thread to process logs off an in-memory queue.

string logFile = "Log.txt";
this.Queue = new ConcurrentQueue<string>();

var thread = new Thread(() => 
{
    string log;

    while (true)
    {
        while (!this.Queue.IsEmpty)
        {
            if (!this.Queue.TryDequeue(out log)) continue;

            File.AppendAllText(logFile, "\n" + log);
        }

        Thread.Sleep(1000);
    }

});

thread.Start();

This implementation doesn't take into account how to cancel the logging thread, so I'll let you attempt that on your own first. I'll also add that this isn't very reliable, given the choice, I'd actually use MSMQ.

6
  • speed is really the issue i have, i need to log extreme fast.
    – Peter
    Apr 22, 2016 at 10:34
  • Out of process queueing seems like a weird solution for an I/O problem to me. I think that would only make the problem bigger. Apr 22, 2016 at 10:34
  • Sure, though you would then be risking losing all the logs stored in memory that were queued for processing, in the event that the app crashes
    – Aydin
    Apr 22, 2016 at 10:35
  • @MarnixvanValen well allready my program needs 8 CPU cores at 3.2Ghz to work, all code is paralized and optimized. There are not always easy answers to real world problems
    – Peter
    Apr 22, 2016 at 10:35
  • 1
    Why don't you try the answer and then get back to us if you run in to issues
    – Aydin
    Apr 22, 2016 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.