Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在多线程应用程序中执行文件日志记录_C#_Multithreading_Logging - Fatal编程技术网

C# 如何在多线程应用程序中执行文件日志记录

C# 如何在多线程应用程序中执行文件日志记录,c#,multithreading,logging,C#,Multithreading,Logging,我需要一种极快的记录方式(关于高速摄像机的帧信息)。 我只需要记录几个数字,还有一个简单的file.log。 为此,事件日志记录会变慢 于是我想,只要创建一个文件流,我就可以为我的应用程序锁定文件。并附加到它 通常我会使用一个简单的行,例如 Filestream fs = new FileStream(@"D:\Log.csv", FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 在方法内部 然而,由于相机驱动程序的帧是在一个新线

我需要一种极快的记录方式(关于高速摄像机的帧信息)。 我只需要记录几个数字,还有一个简单的file.log。 为此,事件日志记录会变慢

于是我想,只要创建一个文件流,我就可以为我的应用程序锁定文件。并附加到它

通常我会使用一个简单的行,例如

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

然而,由于相机驱动程序的帧是在一个新线程中执行的,所以我在这里遇到了一个问题。因为我不想每次写入日志文件时都重新打开和关闭该文件。(打开和关闭缓慢)

我想在程序开始时打开日志文件一次,线程应该只对它执行写入操作,而不是一次又一次地关闭和打开它

如何实现这一点,因为这不起作用:

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));
    }

有很多种方法,但大多数(如果不是全部的话)都涉及排队,特别是在多线程环境中

您可以使用MSMQ对日志进行排队处理,也可以使用单独的线程处理内存队列中的日志

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();
string logFile=“Log.txt”;
this.Queue=新的ConcurrentQueue();
变量线程=新线程(()=>
{
字符串日志;
while(true)
{
而(!this.Queue.IsEmpty)
{
如果(!this.Queue.TryDequeue(out log))继续;
文件.AppendAllText(日志文件“\n”+log);
}
睡眠(1000);
}
});
thread.Start();

这个实现没有考虑如何取消日志线程,所以我让您先自己尝试一下。我还要补充一点,这不是很可靠,考虑到选择,我实际上会使用MSMQ。

“不起作用”是相当不具体的。请澄清实际问题是什么。为什么不使用@LasseV.Karlsen,我尝试了几种组合声明此文件,编译器不接受我如何声明它,ea fs filestream变量在camera_thread.FrameArived中无法识别,因为fs不是全局变量我会使用一些日志库,这一点在NLog和Log4Net等日志框架中得到了解决。既支持异步写入磁盘,又支持从多个线程记录到单个文件。速度确实是我的问题,我需要非常快地记录。进程外排队对我来说似乎是解决i/O问题的一个奇怪的解决方案。我认为这只会使问题变得更大。当然,如果应用程序crashes@MarnixvanValen我的程序需要8个3.2Ghz的CPU核才能工作,所有代码都经过并行化和优化。现实世界中的问题并不总是有简单的答案。如果遇到问题,你为什么不尝试一下答案,然后再联系我们