2012-02-15 69 views
0

MS Visual Studio 2010中的C#我將輸出寫入日誌文件,作爲一些代碼驗證的一部分。調試器顯示組正在匹配,匹配輸出也是如此。唯一的錯誤是輸出文件,我不知道爲什麼。以下是我從文件中獲得的輸出,其中readline是輸入行,Match是Regex匹配的結果,Slot/Port是從正則表達式中獲取命名組值的結果。您可以從日誌中看到正則表達式匹配與輸入行匹配。調試器說,命名組正在工作。日誌文件輸出與調試器不匹配

readline is: fc1/1 is up 
Match is: fc1/1 is 
Slot and Port are: 1/1 
readline is: fc1/3 is up 
Match is: fc1/3 is 
Slot and Port are: 1/1 

這是匹配的代碼 - 我添加了一些額外的東西只是爲了更容易在調試器中觀看。

if (RegexExtensions.TryMatch(out InterfaceMatch, trimmed, InterfaceMatchString)) 
       { 
        SlotNum = Convert.ToInt32(InterfaceMatch.Groups["slot"].Value); 
        PortNum = Convert.ToInt32(InterfaceMatch.Groups["port"].Value); 
        string slot = InterfaceMatch.Groups["slot"].Value; 
        string port = InterfaceMatch.Groups["port"].Value; 
    // write the value of current incoming readline 
        CiscoDecodeVariables.swlog.WriteLine(String.Format("readline is: {0}", readline)); 
// write the value of the current match 
        CiscoDecodeVariables.swlog.WriteLine(String.Format("Match is: {0}", InterfaceMatch.Value.ToString())); 
        string slotasstring = SlotNum.ToString(); 
        string portasstring = PortNum.ToString(); 
    // here is the line that writes what the slot and port values are 
       CiscoDecodeVariables.swlog.WriteLine(String.Format("Slot and Port are: {0}/{0}", slotasstring, portasstring)); 
       CiscoDecodeVariables.swlog.Flush(); 
        //sw.Close(); 
        currPort = ((PortModule)ModuleList[SlotNum]).FCPortList[PortNum - 1]; 
       } 

最後,這裏是StreamWriter的代碼。我使用靜態變量刪除了一個靜態類,以便我可以在需要的地方使用Streamwriter。

static public class CiscoDecodeVariables 
{ 
    static public string LogFileOutPutPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\\ciscodecodelog.txt"; 
    static public StreamWriter swlog = new StreamWriter(LogFileOutPutPath); 

} 

添加有問題 串InterfaceMatchString =正則表達式匹配模式@ 「FC(\ d +?)/(\ d +α)」;

另外,RegexExtensions.TryMatch()是一個靜態方法,它爲成功匹配返回true並將InterfaceMatch實例設置爲結果。

+0

所以你是說如果你停下來寫出匹配的行,並在監視窗口中放入'InterfaceMatch.Value.ToString()',它就會出現一些沒有寫入swlog的東西如你所料?因爲這很奇怪,如果是這樣的話...... – Chris 2012-02-15 15:19:11

+0

是的。調試器看起來不錯。上面的'currPort'行將當前端口分配給正確的端口,即模塊列表中的端口,插槽和端口與我從正則表達式解析出的匹配。我能想到的唯一的事情就是String.Format()正在爲我提供的插槽和端口值做一些事情。 – 2012-02-15 15:21:26

+0

請添加正則表達式和輸入字符串。謝謝!另外,SlotNum和PortNum是什麼類型? – 2012-02-15 15:21:51

回答

1

這是您的格式字符串的問題。

CiscoDecodeVariables.swlog.WriteLine(String.Format("Slot and Port are: {0}/{0}", slotasstring, portasstring)); 

「{0}/{0}」 意味着你會得到結果 「slotasstring/slotasstring」,而不是 「slotasstring/portasstring」。

2

您的代碼:

"Slot and Port are: {0}/{0}" 

正確的代碼:

"Slot and Port are: {0}/{1}" 

易錯字的錯誤,它看起來像。 :)

爲了澄清那些可能不明白第一行是明確地把同樣的事情放在兩次(第一個後續參數),而第二行是放在第一個參數,然後第二個(通過{0}和{1})。

作爲附加說明String.Format接受任何對象,如果需要的話,將調用ToString,因此不需要首先轉換爲字符串(儘管我明白,您可能已將這些行純粹用於調試幫助)。還有一個InterfaceMatch.Value.ToString()我認爲Value應該已經是一個字符串。

+0

經典之作! Resharper會爲你挑選這些錯誤:) – MattDavey 2012-02-15 15:41:45

+0

感謝您對String.Format()的建議。我不知道它會爲我運行ToString()。至於首先轉換爲字符串,是的,這只是爲了調試。這樣我就不必深入挖掘RegEx匹配的值(並且只是爲了驗證int的ToString()是否符合我的期望) – 2012-02-15 18:34:03