2011-04-25 34 views
1

獲取所需的信息,我已經在我的日誌文件以下行最好的辦法從日誌文件

14:40:21.581 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:24.144 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:25.175 MC: <DataContainer> 
<EquipmentHeartbeat dateTime="2011-04-06T14:43:21.00+01:00" interval="300" recipeId="ES-AD0109071F-3C-PS.ASP"/> 
</DataContainer> 
14:40:26.675 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:29.206 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:29.675 INFO MobileConnection: Creating new GlobalLLWData completed, Milliseconds used: 0 
14:40:30.769 SMDMachine.keepAlive() frequency:18 pr second. 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.2.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.5.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.11.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.13.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.14.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.15.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.17.1 (current ReelID=??) 
14:40:31.612 INFO McDevicePosition.ReelMethods.GetSplicingChainInformation(): LazyFetch on position 2.18.1 (current ReelID=??) 
14:40:31.737 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:34.269 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:36.800 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:39.326 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:42.029 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:44.545 In stopmachine. rtfBoxStop=false,secsGemStop=false,ht=false,error=1007, multiBoxStop=false stoptext=null 
14:40:45.764 MC: <DataContainer> 
<EquipmentInformation dateTime="2011-04-06T14:43:42.31+01:00" laneList="1" zoneList="1-3" informationId="ReportPPM"><Extensions><ReportPPM dateTimeStart="2011-04-06T14:43:05.31+01:00" dateTimeEnd="2011-04-06T14:43:42.31+01:00" PcbID="1302014560"><Nozzle headId="1-0-PHHA1" numberOfPickAttempts="8" numberOfPlacements="8"/><Nozzle headId="2-0-PHHA1" numberOfPickAttempts="16" numberOfPlacements="16"/></ReportPPM></Extensions></EquipmentInformation> 
<EquipmentInformation dateTime="2011-04-06T14:43:42.37+01:00" laneList="1" zoneList="1-3" informationId="ReportPPM"><Extensions><ReportPPM dateTimeStart="2011-04-06T14:43:05.37+01:00" dateTimeEnd="2011-04-06T14:43:42.37+01:00" PcbID="1302014560"><MaterialHandler materialSupplyArea="02" trackId="06" feederDivision="001" materialHandlerId="0206.001" materialHandlerIdAlt="2.6.1" feederType="ITF2_24" numberOfPickAttempts="4" numberOfPlacements="4"/><MaterialHandler materialSupplyArea="02" trackId="09" feederDivision="001" materialHandlerId="0209.001" materialHandlerIdAlt="2.9.1" feederType="ITF2_16" numberOfPickAttempts="4" numberOfPlacements="4"/><MaterialHandler materialSupplyArea="02" trackId="11" feederDivision="001" materialHandlerId="0211.001" materialHandlerIdAlt="2.11.1" feederType="ITF2_12" numberOfPickAttempts="4" numberOfPlacements="4"/><MaterialHandler materialSupplyArea="02" trackId="13" feederDivision="001" materialHandlerId="0213.001" materialHandlerIdAlt="2.13.1" feederType="ITF2_12" numberOfPickAttempts="4" numberOfPlacements="4"/><MaterialHandler materialSupplyArea="02" trackId="14" feederDivision="001" materialHandlerId="0214.001" materialHandlerIdAlt="2.14.1" feederType="ITF2_12" numberOfPickAttempts="4" numberOfPlacements="4"/><MaterialHandler materialSupplyArea="02" trackId="16" feederDivision="001" materialHandlerId="0216.001" materialHandlerIdAlt="2.16.1" feederType="ITF2_16" numberOfPickAttempts="4" numberOfPlacements="4"/></ReportPPM></Extensions></EquipmentInformation> 
<EquipmentInformation dateTime="2011-04-06T14:43:42.37+01:00" laneList="1" zoneList="1-3" informationId="ReportPPM"><Extensions><ReportPPM dateTimeStart="2011-04-06T14:43:05.37+01:00" dateTimeEnd="2011-04-06T14:43:42.37+01:00" PcbID="1302014560"><Component componentId="IC0102667" partId="IC0102667F" lotId="" numberOfPickAttempts="4" numberOfPlacements="4"/><Component componentId="IC0102669" partId="IC0102669F" lotId="" numberOfPickAttempts="4" numberOfPlacements="4"/><Component componentId="IC0102665" partId="IC0102665F" lotId="" numberOfPickAttempts="4" numberOfPlacements="4"/><Component componentId="IC0102958" partId="IC0102958F" lotId="" numberOfPickAttempts="4" numberOfPlacements="4"/><Component componentId="IC0102671" partId="IC0102671F" lotId="" numberOfPickAttempts="4" numberOfPlacements="4"/><Component componentId="XT0100253" partId="XT0100253F" lotId="" numberOfPickAttempts="4" numberOfPlacements="4"/></ReportPPM></Extensions></EquipmentInformation> 
<EquipmentBlocked dateTime="2011-04-06T14:43:42.37+01:00"/> 
<EquipmentChangeState dateTime="2011-04-06T14:43:42.37+01:00" currentState="READY-IDLE-BLOCKED" previousState="READY-PROCESSING-EXECUTING" eventId="EquipmentBlocked"><Extensions currentSEMI-State="SBY/No product/Blocked" previousSEMI-State="PRD/Regular production/Process product"/></EquipmentChangeState> 
<EquipmentErrorsCleared dateTime="2011-04-06T14:43:42.37+01:00"/> 
<EquipmentChangeState dateTime="2011-04-06T14:43:42.37+01:00" currentState="READY-IDLE-BLOCKED" previousState="READY-IDLE-BLOCKED" eventId="EquipmentErrorsCleared"><Extensions currentSEMI-State="SBY/No product/Blocked" previousSEMI-State="PRD/Regular production/Process product"/></EquipmentChangeState> 
<EquipmentAlarmsCleared dateTime="2011-04-06T14:43:42.37+01:00"/> 
<EquipmentChangeState dateTime="2011-04-06T14:43:42.37+01:00" currentState="READY-IDLE-BLOCKED" previousState="READY-IDLE-BLOCKED" eventId="EquipmentAlarmsCleared"><Extensions currentSEMI-State="SBY/No product/Blocked" previousSEMI-State="PRD/Regular production/Process product"/></EquipmentChangeState> 
<ItemTransferIn dateTime="2011-04-06T14:43:05.91+01:00" itemInstanceId="1302014560" laneId="1"/> 
<ItemTransferZone dateTime="2011-04-06T14:43:05.91+01:00" itemInstanceId="1302014560" fromZoneId="1" toZoneId="2" laneId="1"/> 
<ItemWorkStart dateTime="2011-04-06T14:43:05.91+01:00" itemInstanceId="1302014560" laneId="1" zoneId="1-3" recipeId="ES-AD0109071F-3C-PS.ASP" orderId="asad"/> 
<ItemWorkAbort dateTime="2011-04-06T14:43:42.77+01:00" itemInstanceId="1302014560" laneId="1" zoneId="2" abortId="Incomplete" cycleTime="35922" recipeId="ES-AD0109071F-3C-PS.ASP" orderId="asad"/> 
<ItemTransferZone dateTime="2011-04-06T14:43:42.77+01:00" itemInstanceId="1302014560" fromZoneId="2" toZoneId="3" laneId="1"/> 
<ItemTransferOut dateTime="2011-04-06T14:43:42.77+01:00" itemInstanceId="1302014560" laneId="1"><Extensions><itemInfo itemTransferInTime="2011-04-06T14:43:05.91+01:00" itemTransferOutTime="2011-04-06T14:43:42.77+01:00" cycleTime="35922" recipeId="ES-AD0109071F-3C-PS.ASP" orderId="asad" itemInstanceId="1302014560" statusId="Incomplete"/></Extensions><Extensions><machineConfig flowlineName="AX-1" machineName="1-1-AX-1_AX201" machineManufacturer="Assembleon" machineModel="AX-201" machineSerial="DC606" machineVersion="n.a." machineSoftwareVersion="3.10_930_26"/></Extensions><Extensions><PlacementSummary totalPlaced="" totalAttempts=""/></Extensions></ItemTransferOut> 
<EquipmentStarved dateTime="2011-04-06T14:43:42.84+01:00"/> 
<EquipmentChangeState dateTime="2011-04-06T14:43:42.84+01:00" currentState="READY-IDLE-STARVED" previousState="READY-IDLE-BLOCKED" eventId="EquipmentStarved"><Extensions currentSEMI-State="SBY/No product/Starved" previousSEMI-State="SBY/No product/Blocked"/></EquipmentChangeState> 
<EquipmentErrorsCleared dateTime="2011-04-06T14:43:42.84+01:00"/> 
<EquipmentChangeState dateTime="2011-04-06T14:43:42.84+01:00" currentState="READY-IDLE-STARVED" previousState="READY-IDLE-STARVED" eventId="EquipmentErrorsCleared"><Extensions currentSEMI-State="SBY/No product/Starved" previousSEMI-State="SBY/No product/Blocked"/></EquipmentChangeState> 
<EquipmentAlarmsCleared dateTime="2011-04-06T14:43:42.84+01:00"/> 
<EquipmentChangeState dateTime="2011-04-06T14:43:42.84+01:00" currentState="READY-IDLE-STARVED" previousState="READY-IDLE-STARVED" eventId="EquipmentAlarmsCleared"><Extensions currentSEMI-State="SBY/No product/Starved" previousSEMI-State="SBY/No product/Blocked"/></EquipmentChangeState> 
</DataContainer> 
14:40:45.764 Curr State=READY-IDLE-STARVED; Curr SemiState=SBY/No product/Starved; Curr Event Id=EquipmentAlarmsCleared 
14:40:45.764 INFO Changing status to WaitBoard 

什麼都將在這裏得到了當時所有的情況下,從存儲之間的數據信息的最佳方式日誌文件作爲一些tupple?我在日誌中有很多回報,我需要抓住所有這些。

我想用indexof字符串函數來做,但它太複雜了。 也許這是一個好主意,在這裏使用正則表達式? (這個問題我有一個關於正則表達式沒有任何線索)

+1

從這個日誌中,您只需要使用''兩個條目?我們可以假設開標籤和時間在同一條線上,並且結束標籤在自己的行上? – svick 2011-04-25 12:17:52

+0

@svick:我希望將所有條目作爲一些元組列表<當時,datacontainer之間是什麼>,並且您的兩個假設都可以。 – 2011-04-25 13:23:24

回答

2

你可以做這樣的:

static readonly Regex DataContainerRegex = 
    new Regex(@"^(\d\d:\d\d:\d\d\.\d\d\d) MC: (<DataContainer>.*?</DataContainer>)", 
       RegexOptions.Singleline | RegexOptions.Multiline); 

static IEnumerable<Tuple<DateTime, XDocument>> Parse(string data) 
{ 
    var matches = DataContainerRegex.Matches(data); 

    return from Match match in matches 
      let date = DateTime.Parse(match.Groups[1].Value, 
            CultureInfo.InvariantCulture) 
      let doc = XDocument.Parse(match.Groups[2].Value) 
      select Tuple.Create(date, doc); 
} 

如果你真的想<DataContainer>而不是XDocument之間的文本,只需將括號周圍.*?直接和解析第二組。

0

這是我在分析樣本日誌文件的嘗試:

static IEnumerable<Tuple<DateTime, string>> GetLogEntries(string path) 
{ 
    var regex = new Regex(@"^(\d{2}\:\d{2}\:\d{2}\.\d{3}\s)(.*)", RegexOptions.Compiled); 

    var logLineBuilder = new StringBuilder(); 
    Match currentMatch = null; 

    foreach(var line in File.ReadLines(path)) 
    { 
     var match = regex.Match(line); 

     if (match.Success && logLineBuilder.Length != 0) 
     { 
      yield return Tuple.Create(DateTime.Parse(currentMatch.Groups[1].Value), logLineBuilder.ToString()); 
      logLineBuilder.Clear(); 
     } 

     if (match.Success) 
      currentMatch = match; 

     logLineBuilder.AppendLine(match.Success ? match.Groups[2].Value : line); 
    } 

    yield return Tuple.Create(DateTime.Parse(currentMatch.Groups[1].Value), logLineBuilder.ToString()); 
} 

我想日誌文件會很大所以我用File.ReadLines將一次讀取日誌文件一行。這應該減少內存佔用,因爲它不必將整個文件讀入內存。這可能是特別大的日誌文件的問題。然後,您應該能夠使用LINQ表達式篩選任何附加標準例如:

var entries = GetLogEntries("logfile.txt"); 

var dataContainers = from e in entries 
        where e.Item2.IndexOf("MC: <DataContainer>") != -1 
        select XDocument.Parse(e.Item2.Substring(3)); 

Console.WriteLine(dataContainers.Count()); //prints "2"