2013-12-09 26 views
0

我指示進程的標準輸出/錯誤,並將其存儲在一個字符串中,我登錄到一個文件。字符串太小,無法存儲進程的標準輸出

我注意到並非所有的標準輸出都存儲在文本文件中,所以我必須達到字符串的大小限制。

有誰知道有助於解決這個問題的替代方案嗎?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace Logging 
{ 
    class Logger 
    { 
     private FileStream file = null; 
     private StreamWriter writer = null; 

     public Logger(string _path) 
     { 
      if (!File.Exists(_path)) File.Create(_path).Dispose(); 

      file = new FileStream(_path, FileMode.Append, FileAccess.Write, FileShare.Write); 
      writer = new StreamWriter(file); 
     } 

     public void Log(string _text) 
     { 
      Console.WriteLine(_text); 
      using (file) 
      { 
       writer.WriteLine("[" + DateTime.Now + "] : " + _text); 
      } 
     } 
    } 
} 
+3

沒有爲字符串不理智的大小限制。 (你的過程不是寫2GB的輸出) – SLaks

+2

在我看來,這可能是你沒有沖洗這個作者。 –

+2

我的心理調試技巧告訴我,你正在吞噬所有的異常而不讀取它們。不要這樣做。 – SLaks

回答

1

string類型的限制爲2GB。

您對文件創建和處理的處理會造成一些困難。

試試這個一點點清潔記錄代碼:

 const string log = @"logfile path"; 
     public void Log(string _text) 
     { 
      try 
      { 
       using (TextWriter tw = new StreamWriter(_path, true)) 
       { 
        tw.WriteLine("[" + DateTime.Now + "] : " + _text); 
       } 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 
5
 using (file) 

你只需設置你的文件。你不能再寫信給它了。

相關問題