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實例設置爲結果。
所以你是說如果你停下來寫出匹配的行,並在監視窗口中放入'InterfaceMatch.Value.ToString()',它就會出現一些沒有寫入swlog的東西如你所料?因爲這很奇怪,如果是這樣的話...... – Chris 2012-02-15 15:19:11
是的。調試器看起來不錯。上面的'currPort'行將當前端口分配給正確的端口,即模塊列表中的端口,插槽和端口與我從正則表達式解析出的匹配。我能想到的唯一的事情就是String.Format()正在爲我提供的插槽和端口值做一些事情。 – 2012-02-15 15:21:26
請添加正則表達式和輸入字符串。謝謝!另外,SlotNum和PortNum是什麼類型? – 2012-02-15 15:21:51