[方法] プログラムによって CSV ファイルにクロール履歴をエクスポートする
Microsoft Office SharePoint Server 2007 でのエンタープライズ検索 では以下のようなクロール統計が管理されます。
クロールの種類 (フルまたは増分)
クロールの開始時刻および終了時刻
クロールに関連付けられているコンテンツ ソース
正常にクロールされたアイテム数および発生した警告とエラーの数
検索管理オブジェクト モデルを使用してこの情報を取得するには、Microsoft.Office.Server.Search.Administration.CrawlHistory クラスを使用します。このクラスには DataTable オブジェクトにクロール統計を返す GetCrawlHistory 関数が含まれます。
以下の手順では、クロール履歴表の内容をカンマ区切り (.csv) ファイルに書き出す方法を示します。その後、ファイルを Microsoft Office Excel などのアプリケーションで開いて、さらに分析することができます。
コンソール アプリケーションからクロール履歴を .csv ファイルにエクスポートするには
Microsoft Visual Studio の [ファイル] メニューで [新規作成] をポイントし、[プロジェクト] をクリックします。
[プロジェクトの種類] で、[C#] の [Windows] をクリックします。
[テンプレート] で [コンソール アプリケーション] をクリックします。[名前] フィールドに「CrawlHistorySample」と入力し、[OK] をクリックします。
[プロジェクト] メニューの [参照の追加] をクリックします。
[.NET] タブで、以下の参照を選択した後、[OK] をクリックします。
Microsoft Search component (Microsoft.Office.Server.Search.dll)
Microsoft Office Server component (Microsoft.Office.Server.dll)
Windows SharePoint Services (Microsoft.SharePoint.dll)
コンソール アプリケーションのクラス ファイルで、他の名前空間ディレクティブを含むコードの上部付近に次の using ステートメントを追加します。
using System.IO; using System.Data; using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
利用状況情報をコンソール ウィンドウに書き出す関数を作成します。
static void Usage() { Console.WriteLine("Export Crawl History to CSV File Sample"); Console.WriteLine("Usage: CrawlHistorySample.exe SiteURL OutputPath OutputName"); Console.WriteLine(@"Example: CrawlHistorySample.exe http://MySearchServer c:\logfiles\ CrawlHistoryLog1"); }
try ブロックを開くために、コンソール アプリケーションの Main 関数に以下のコードを追加します。
try {
args[] パラメータのアイテム数を確認するコードを追加します。アイテム数は 3 であることが必要です。そうでない場合は、手順 7. で定義される Usage 関数を呼び出します。
if (args.Length != 3) { Usage(); return; }
args[] パラメータに指定される値を取得します。この値は、検索センター サイト URL, .csv ファイルの出力パス、および名前を指定するために使用されます。
string strURL = args[0]; string csvPath = args[1] + "\\" + args[2] + ".csv";
SearchContext を取得するために以下のコードを追加します。
SearchContext context; using (SPSite site = new SPSite(strURL)) { context = SearchContext.GetContext(site); }
以下のコードを使用してクロール履歴を [DataTableT:System.Data.DataTable] オブジェクトに取得します。
CrawlHistory history = new CrawlHistory(context); DataTable table = new DataTable(); table = history.GetCrawlHistory();
[StreamWriterT:System.IO.StreamWriter] オブジェクトを初期化するために以下のコードを追加します。
using (StreamWriter writer = new StreamWriter(csvPath)) { /* Code to iterate through DataTable rows, and write out the values to the .csv file See steps 14 through 17. */
以下のように列数を取得します。
int colCount = table.Columns.Count;
各列名の後にカンマを付けて (最後の列名の後には付けない) ファイル ストリームに書き出し、その後、ファイル ストリームを改行します。
for (int i = 0; i < colCount; i++) { writer.Write(table.Columns[i].ToString()); if (i < colCount - 1) { writer.Write(","); } } writer.Write(writer.NewLine);
行のコレクションをループ処理し、各列値をカンマで区切ってファイル ストリームに書き出します。各行の最後でファイル ストリームを改行して、次の行に進みます。
foreach (DataRow row in table.Rows) { for (int i = 0; i < colCount; i++) { writer.Write(row[i].ToString()); if (i < colCount - 1) { writer.Write(","); } } writer.Write(writer.NewLine); }
以下のように、ファイル ストリームの内容をフラッシュして閉じ、using ステートメント ブロックと try ブロックを閉じます。
writer.Flush(); writer.Close(); } }
以下のコードを catch ブロックに追加します。
catch (Exception ex) { Console.WriteLine(ex.ToString()); Usage(); }
例
以下に、CrawlHistorySample コンソール アプリケーション クラスの完全なコードを示します。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
namespace CrawlHistorySample
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length != 3)
{
Usage();
return;
}
string strURL = args[0];
string csvPath = args[1] + "\\" + args[2] + ".csv";
SearchContext context;
using (SPSite site = new SPSite(strURL))
{
context = SearchContext.GetContext(site);
}
CrawlHistory history = new CrawlHistory(context);
DataTable table = new DataTable();
table = history.GetCrawlHistory();
using (StreamWriter writer = new StreamWriter(csvPath, false))
{
int colCount = table.Columns.Count;
for (int i = 0; i < colCount; i++)
{
writer.Write(table.Columns[i].ToString());
if (i < colCount - 1)
{
writer.Write(",");
}
}
writer.Write(writer.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < colCount; i++)
{
writer.Write(row[i].ToString());
if (i < colCount - 1)
{
writer.Write(",");
}
}
writer.Write(writer.NewLine);
}
writer.Flush();
writer.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Usage();
}
}
static void Usage()
{
Console.WriteLine("Export Crawl History to CSV File Sample");
Console.WriteLine("Usage: CrawlHistorySample.exe SiteURL OutputPath OutputName");
Console.WriteLine(@"Example: CrawlHistorySample.exe http://MySearchServer c:\logfiles\ CrawlHistoryLog1");
}
}
}