【问题标题】:C# TextWriterTraceListener last lines missing from fileC# TextWriterTraceListener 文件中缺少最后几行
【发布时间】:2011-03-23 11:11:14
【问题描述】:

当侦听器完成写入文件时,有没有办法等待。现在一半的数据被切断了。我认为这是因为 TextWriterTraceListener 在应用程序关闭之前设法将数据写入文件。

    static void Main(string[] args)
    {
        Stopwatch stopwatch = new Stopwatch();

        // Begin timing
        stopwatch.Start();

        try
        {
            Trace.Listeners.Add(new ConsoleTraceListener(false));

            Trace.WriteLine(" ");
            Trace.WriteLine(DateTime.Now);
            Trace.WriteLine("Starting application...");
            Trace.WriteLine("-----------------------------------------------------------");
            Trace.WriteLine("Elapsed ms | Task information");
            Trace.WriteLine("-----------------------------------------------------------");
            Console.ForegroundColor = ConsoleColor.Green;

            foreach (int i in Enumerable.Range(1, 3))
            {
                Trace.WriteLine("Keypair nr: " + i);
                Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | Starting to generate bytes for wrapping key..");
                byte[] keyBytes = generateKeyBytes();
                Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | ..generation finished.");
                Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | Executing java to generate private and public keys..");
                string keysString = runJava(keyBytes);
                Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | ..generation finished.");
                string[] keys = keysString.Split(',');

                byte[] pubKey = Convert.FromBase64String(keys[0]);
                byte[] wrappedKey = Convert.FromBase64String(keys[1]);

                Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | Decrypting the private key..");
                byte[] privKey = Decrypt(wrappedKey, keyBytes);
                Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | ..decryption finished.");

                Trace.WriteLine("Public Key:\n\r" + BitConverter.ToString(pubKey));
                Trace.WriteLine("Private Key:\n\r" + BitConverter.ToString(privKey));
            }
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Trace.WriteLine("From: " + ex.Source + ".\nDetail: "+ ex.Message);
        }
        finally
        {
            Trace.WriteLine("Total Elapsed time: " + stopwatch.Elapsed);
            stopwatch.Stop();
            Console.WriteLine("Any key to exit..");
            Console.ReadKey();
        }
    }

添加了我的代码。 在配置文件中我有:

<system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener"
          type="System.Diagnostics.TextWriterTraceListener"
          initializeData="output.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>

打印出来的最后一个字节数组被截断了,finnaly 块中的行在 output.log 中也丢失了,但它们仍然出现在控制台上。

【问题讨论】:

  • 而 AutoFlush 设置为 ....
  • 我使用了两个监听器。其他是 ConsoleTraceListener 在控制台上写入信息,这工作正常。一切都被写出来。但在文件中,最后一行只是丢失了。

标签: c# listener


【解决方案1】:

Trace 和 Debug 类具有 Flush、Close 和 AutoFlush 成员。您需要至少使用其中之一。 ConsoleListener 在没有它们的情况下工作是偶然的。

【讨论】:

    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2019-01-21
    • 2014-06-22
    • 2012-05-23
    相关资源
    最近更新 更多