如何:以编程方式将爬网历史记录导出成 CSV 文件

Microsoft Office SharePoint Server 2007 企业级搜索跟踪爬网统计信息,其中包括:

  • 爬网类型(完全或增量)

  • 爬网开始时间和结束时间

  • 与爬网关联的内容源

  • 成功爬网的项数以及出现的警告数和错误数

可以使用搜索管理对象模型通过 Microsoft.Office.Server.Search.Administration.CrawlHistory 类来检索此信息。此类包含 GetCrawlHistory 函数,该函数将返回 DataTable 对象中的爬网统计信息。

以下过程演示如何将爬网历史记录表的内容写入到一个逗号分隔值 (.csv) 文件中。然后您可以在诸如 Microsoft Office Excel 之类的应用程序中打开该文件以便进一步分析。

通过控制台应用程序将爬网历史记录导出到 .csv 文件

  1. 在 Microsoft Visual Studio 的“文件”菜单上,指向“新建”,然后单击“项目”。

  2. 在“项目类型”中,在“C#”下单击“Windows”。

  3. 在“模板”下,单击“控制台应用程序”。在“名称”字段中,键入“CrawlHistorySample”,然后单击“确定”。

  4. 在“项目”菜单上单击“添加引用”。

  5. 在“.NET”选项卡上,选择以下引用,然后单击“确定”:

    • Microsoft Search 组件 (Microsoft.Office.Server.Search.dll)

    • Microsoft Office Server 组件 (Microsoft.Office.Server.dll)

    • Windows SharePoint Services (Microsoft.SharePoint.dll)

  6. 在控制台应用程序的类文件中,将以下 using 语句添加到具有其他命名空间指令的代码顶部附近。

    using System.IO;
    using System.Data;
    using Microsoft.SharePoint;
    using Microsoft.Office.Server.Search.Administration;
    
  7. 创建用于向控制台窗口写入使用信息的函数。

    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");
    }
    
  8. 在控制台应用程序的 Main 函数中,添加以下代码以打开 try 块。

    try
    {
    
  9. 添加代码以检查 args[] 参数中的项数;此数字必须等于 3。如果不是,则调用在步骤 7 中定义的 Usage 函数。

    if (args.Length != 3)
    {
       Usage();
       return;
    }
    
  10. 检索在 args[] 参数中指定的值,这些值将用于指定搜索中心网站 URL、输出路径和 .csv 文件的名称。

    string strURL = args[0];
    string csvPath = args[1] + "\\" + args[2] + ".csv";
    
  11. 添加以下代码以检索 SearchContext

    SearchContext context;
    using (SPSite site = new SPSite(strURL))
    {
       context = SearchContext.GetContext(site);
    }
    
  12. 使用以下代码将爬网历史记录检索到 [DataTableT:System.Data.DataTable] 对象中。

    CrawlHistory history = new CrawlHistory(context);
    DataTable table = new DataTable();
    table = history.GetCrawlHistory();
    
  13. 添加以下代码以初始化 [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.
    */
    
  14. 检索列的数目,如下所示。

    int colCount = table.Columns.Count;
    
  15. 将列名称写入到文件流中(每个名称后面有一个逗号,最后一个列名称除外),然后将新行写入到文件流中,如下所示。

    for (int i = 0; i < colCount; i++)
    {
       writer.Write(table.Columns[i].ToString());
       if (i < colCount - 1)
       {
          writer.Write(",");
       }
    }
    writer.Write(writer.NewLine);
    
  16. 遍历行的集合并将每个列值写入到文件流中(每个值后面都有一个逗号)。在移动到下一行之前,请将新行写入到文件流中该行的末尾。

    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);
    }
    
  17. 刷新文件流的内容,关闭它,然后再关闭 using 语句块和 try 块,如下所示。

          writer.Flush();
          writer.Close();
       }
    }
    
  18. 为 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");
        }
    }
}

See Also

任务

如何:返回搜索服务提供程序的搜索上下文

概念

企业级搜索管理对象模型入门