2012-04-01 18 views
2

WMIステートメントでWMIが取得するエントリの数を制限する方法はありますか? これは、すべてのWin32_NTLogEventインスタンスを取得するためのクエリの実行が永遠に行われているためです。私が本当に必要とするのは、最新のイベントです(約1週間、または2000エントリ)。WMIが遅すぎる

ログデータを取得するために使用しているコードのスニペットです。 Win32_Processorのような他のクエリは素早く素早く行うことができます。

public static ManagementObject[] GetWMIData(string wmiClass) { return GetWMIData(wmiClass, "", "CIMV2"); } 
    public static ManagementObject[] GetWMIData(string wmiClass, string whereClause) { return GetWMIData(wmiClass, whereClause, "CIMV2"); } 
    public static ManagementObject[] GetWMIData(string wmiClass, string whereClause, string nameSpace) 
    { 
     try 
     { 
      // If a where clause has been set, prepare the clause to add to the query string 
      if (whereClause != "") 
      { 
       whereClause = " WHERE " + whereClause; 
      } 
      // Create a search query 
      string query = "SELECT * FROM " + wmiClass + whereClause; 
      ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher("root\\" + nameSpace, query); 
      ManagementObjectCollection matches = wmiSearcher.Get(); 

      // Create an array to hold the matches 
      ManagementObject[] matchArray = new ManagementObject[matches.Count]; 

      // If matches found, copy to output 
      if(matches.Count > 0) 
      { 
       // Copy the search matches into this array 
       matches.CopyTo(matchArray, 0); 
      } 

      // Return array 
      return matchArray; 
     } 
     catch (Exception e) 
     { 
      ErrorDialogue errorReporter = new ErrorDialogue(e); 
      return null; 
     } 
    } 
各ログが保存されます

public class Log 
{ 
    public string Category = "N/A"; 
    public string DateTime = "N/A"; 
    public UInt16 ID = 0; 
    public string Level = "N/A"; 
    public string Message = "N/A"; 
    public string Source = "N/A"; 

    public Log() { } 
    public Log(ManagementObject wmiLogEvent) 
    { 
     this.GetInfo(wmiLogEvent); 
    } 

    public void GetInfo(ManagementObject wmiLogEvent) 
    { 
     try 
     { 
      this.Category = DataRetriever.GetValue(wmiLogEvent, "CategoryString"); 
      this.DateTime = DataRetriever.GetValue(wmiLogEvent, "TimeGenerated"); 
      this.ID = DataRetriever.GetValueUInt16(wmiLogEvent, "EventIdentifier"); 
      this.Level = DataRetriever.ConvertEventType(DataRetriever.GetValueUInt16(wmiLogEvent, "CategoryString")); 
      this.Message = DataRetriever.GetValue(wmiLogEvent, "Message"); 
      this.Source = DataRetriever.GetValue(wmiLogEvent, "SourceName"); 
     } 
     catch (Exception e) 
     { 
      ErrorDialogue errorReporter = new ErrorDialogue(e); 
     } 
    } 
} 
+0

、それはmatches.Countを使用する2つの場所があることに注意してください。これにより、列挙が毎回先頭に巻き戻され、繰り返し処理されてカウントが取得され、元の位置にリセットされます。 (RewindableオプションをFalseに設定することでこれを証明できます)。コード "if(matches.Count> 0)"を "if(matchArray.Length> 0)"に変更するのが2倍高速です。 –

答えて

4

一つの選択肢は、範囲を指定するWHERE句を使用することですが、次のように

  if (Configuration.OnlyErrorLogs) 
      { 
       // If Information logs should be suppressed, only get events where event type is not 3 
       WMIDataTemp1 = DataRetriever.GetWMIData("Win32_NTLogEvent", "EventType<>3"); 
      } 
      else 
      { 
       WMIDataTemp1 = DataRetriever.GetWMIData("Win32_NTLogEvent"); 
      } 
      foreach (ManagementObject Object in WMIDataTemp1) 
      { 
       this.Log.Add(new Log(Object)); 
      } 

そして、WMIデータを取得するための機能がありますあなたが望むエントリの...

たとえば、を使用することができますWHERE句で0を指定して...

ManagementObjectSearcherを作成するときには、BlockSizeを設定することもできます。

たとえば、コールごとに2000エントリを指定するには、これを使用して、ORDER BY TimeGenerated DESCと一緒にすればよい結果が得られます。

+0

'BlockSize'を拡張できますか?どのように動作するのでしょうか?それは何を達成するのですか? そして 'WHERE'節に何を追加できますか? – CJxD

+0

デフォルトでは、Windows 7のログサイズは20megなので、20mg分のデータを列挙してWMIに戻すには時間がかかることがあります。 –

+0

@ErikPhilips Granted;ファイルを手動で読み込んでデータコンテナに編成する方法はありますか?コンテナクラスの外観を投稿します。 – CJxD

1

スピードはWMIの強力なスーツではありません。それはかなりメモリ集中的な傾向があります。しかし、問題は解決されており、あなたができることがいくつかあります。 Microsoft TechNetのWhy are my queries taking such a long time to complete?を参照してください。

+0

私が見る限り、検索するアイテムの数を減らすためにLIMITステートメントのようなものは何もありません。 – CJxD

+0

正しい。クエリを「制限する」方法はありません。ただし、データの繰り返し処理を何度も繰り返す必要がない場合は、少しオーバーヘッドを節約する半同期方式を試すこともできます。 – Nilpo

関連する問題