2014-03-01 22 views
0

服務響應我有一個WCF服務。我有這個操作記錄了服務中的任何請求。我在這裏遇到的問題是它將每個請求記錄在單獨的文件中。如何將所有日誌記錄在單個文件中,並將每個日誌與單行分開?如何登錄一個文件

我想記錄它在一個文件中的性能的原因。

 public byte sendMessage(string strMsgId, string strMessage) 
     { 
     byte result = 1; 
     try 
     { 
      if (strMsgId == "02") 
      { 
       // Logging--------------------------------------------------------------------------------- 
       // Build timestamp string 
       var currentDateTime = DateTime.Now; 
       string timeStampString = currentDateTime.ToString("yyyy-MM-dd hhmmssfff"); 

       // Build filename for Inbound messages, concat timestamp and .txt extension. 
       string debugFileName = "C:\\Inboundmessage" + " " + timeStampString + ".txt"; 
       var inboundMessageLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default); 
       // Write to the file: 
       inboundMessageLog.WriteLine(DateTime.Now); 
       inboundMessageLog.WriteLine("Time = {0}", currentDateTime.ToString("yyyy-MM-dd hh:mm:ss:fff tt")); 
       inboundMessageLog.WriteLine("{0}{1}{2}", "Inbound Message:", Environment.NewLine, strMessage.Substring(1016, 26)); 
       inboundMessageLog.Close(); 

       // ------------------------------------------------------------------------------------------- 
       result = 0; 
      } 

     } 
     catch 
     { 
      //Failed 
      result = 1; 
     } 
     return result; 
+0

登錄到數據庫來代替。 ;) – zimdanen

回答

0

做的@Sudhakar和喬爾說,在using contruct包裹​​StreamWriter

using (var inboundMessageLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default)) { 
    // Write to the file: 
    inboundMessageLog.WriteLine(DateTime.Now); 
    inboundMessageLog.WriteLine("Time = {0}", currentDateTime.ToString("yyyy-MM-dd hh:mm:ss:fff tt")); 
    inboundMessageLog.WriteLine("{0}{1}{2}", "Inbound Message:", Environment.NewLine, strMessage.Substring(1016, 26)); 
} 
0

改變這一行從:

string debugFileName = "C:\\Inboundmessage" + " " + timeStampString + ".txt"; 
var inboundMessageLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default); 

到:

string debugFileName = "C:\\Inboundmessage.txt"; 
var inboundMessageLog = new StreamWriter(debugFileName, true, System.Text.Encoding.Default); 

參見MSDN

public StreamWriter(
    string path, 
    bool append, 
    Encoding encoding 
) 

追加類型:System.Boolean到數據追加到該文件;假到 覆蓋該文件。如果指定的文件不存在,則此 參數不起作用,並且構造函數將創建一個新文件。

0

問題:

在當前的代碼要更改與每個日誌條目最新時間戳的文件名。

StreamWriter()方法首先檢查指定的文件是否存在,如果該文件不存在,它會創建一個新的文件。

你的情況

因爲你正在改變每個日誌條目可能notbe發現文件名,並創建新的文件每次。

解決方案1:

您可以創建一個變量文件(debugFileName)的路徑存儲在固定的文件名(例如:myLog.txt)上面的函數作爲類變量,並使用相同的您sendMessage()功能改變StreamWriter()構造的追加屬性true的內容附加,而不是覆蓋該文件的舊個內容爲每個日誌條目。

從MSDN:StreamWriter Constructor (String, Boolean, Encoding)

通過使用指定的編碼和默認緩衝區大小初始化爲指定 文件StreamWriter類的新實例。如果 文件存在,它可以被覆蓋或附加到。如果文件 不存在,則此構造函數將創建一個新文件。

建議:您使用StremWriter它實現了IDisposable接口,以便在出現using {}塊內包圍它的聲明,以確保離開使用塊後其(對象)的處置。

注:如果您正在使用使用{}塊你不需要調用StreamWriter對象上Close()功能,因爲它會通過using{}塊被照顧。

試試這個:

string debugFileName = "C:\\myLog.txt";//declare this as class variable 
public byte sendMessage(string strMsgId, string strMessage) 
    { 
    byte result = 1; 
    try 
    { 
     if (strMsgId == "02") 
     {     
      // Build timestamp string 
      var currentDateTime = DateTime.Now; 
      string timeStampString = currentDateTime.ToString("yyyy-MM-dd hhmmssfff"); 
      using(var inboundMessageLog = new StreamWriter(debugFileName, true, System.Text.Encoding.Default)) 
      { 
      // Write to the file: 
      inboundMessageLog.WriteLine(DateTime.Now); 
      inboundMessageLog.WriteLine("Time = {0}", currentDateTime.ToString("yyyy-MM-dd hh:mm:ss:fff tt")); 
      inboundMessageLog.WriteLine("{0}{1}{2}", "Inbound Message:", Environment.NewLine, strMessage.Substring(1016, 26)); 


      // ------------------------------------------------------------------------------------------- 
      result = 0; 
      } 
     } 
    } 
    catch 
    { 
     //Failed 
     result = 1; 
    } 
    return result; 
    } 

解決方案2:如果你不想對付你可以簡單地使用靜態類有System.IO.File類寫入文件內容物處置問題。

string debugFileName = "C:\\myLog.txt"; //declare this as class variable 
public byte sendMessage(string strMsgId, string strMessage) 
    { 
    StringBuilder strLines=new StringBuilder(); 
    byte result = 1; 
    try 
    { 
     if (strMsgId == "02") 
     {     
      // Build timestamp string 
      var currentDateTime = DateTime.Now; 
      string timeStampString = currentDateTime.ToString("yyyy-MM-dd hhmmssfff"); 

      // Write to the file: 
      strLines.Append(DateTime.Now+Environment.NewLine); 
      strLines.Append("Time = {0}"+Environment.NewLine, currentDateTime.ToString("yyyy-MM-dd hh:mm:ss:fff tt")); 
      strLines.Append("{0}{1}{2}"+Environment.NewLine, "Inbound Message:", Environment.NewLine, strMessage.Substring(1016, 26));     

      File.WriteAllText(debugFileName , str.ToString()); 
      result = 0; 
      } 

    } 
    catch 
    { 
     //Failed 
     result = 1; 
    } 
    return result; 
    } 
0

上述回覆將解決您的問題。從長遠來看,打擊你的另一個問題將是因爲你打破了單一責任原則

WCF服務應該只是爲請求服務。日誌記錄是一種不同的功能。

如果我一直和你一起工作,我會建議將日誌記錄一起放到不同的類中,並在服務中留下抽象。

public interface ILogger 
{ 
    bool LogMessage(string message, SeverityEnum severity..etc); 
} 

public class Service: IService 
{ 
    ILogger logger; 
    public Service(ILogger logger) 
    { 
      //check if logger is not null 
      this.logger = logger; 
    } 
} 

使用WCF的擴展來插入記錄器依賴項並將登錄功能從WCF中分離出來。

您需要一個服務主機工廠和Dependeny Injection來實現其餘的功能。

但是,一旦你完全控制了記錄如何爲你工作。

所有最優秀的

齊亞

相關問題