由於最近又要對 IIS日誌 (Log) 分析,以便得出各個搜尋引擎每日抓取的頻率,所以這兩天一直在嘗試各個辦法來分析 IIS 日誌 (Log),其中嘗試過:匯入資料庫、Log parser、Powsershell 等等方法,最後改用的是c# 讀取 IIS 日誌的方法,效能最好,定製化也比較能滿足需求。
讀取 100M 的 log日誌,大概10幾秒就能完成,下面是一個讀取IISlog日誌分析各個爬蟲來的數量的例子:
//百度爬蟲標識符號 : Baiduspider
//Google爬蟲標識符號 : Googlebot
//搜狗爬蟲標識符號 : Sogou+web+spider
//搜搜爬蟲標識符號 : Sosospider
private void button1_Click(object sender, EventArgs e)
{
int Baidubot = 0, Googlebot = 0, Sogoubot = 0, Sosobot = 0;
//log 日誌的目錄
string url = textBox1.Text.Trim();
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
#region 迴圈讀取文本,並統計各個爬蟲次數
using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default))
{
string line = string.Empty;
while (!string.IsNullOrEmpty(line = sr.ReadLine()))
{
if (line.Contains("Baiduspider"))
{
++Baidubot;
}
else if (line.Contains("Googlebot"))
{
++Googlebot;
}
else if (line.Contains("Sogou+web+spider"))
{
++Sogoubot;
}
else if (line.Contains("Sosospider"))
{
++Sosobot;
}
}
}
#endregion
label2.Text = "搜尋引擎光顧次數:\n\r\n\r";
label2.Text += "百度:" + Baidubot + "\n\r\n\r";
label2.Text += "Google:" + Googlebot + "\n\r\n\r";
label2.Text += "搜狗:" + Sogoubot + "\n\r\n\r";
label2.Text += "搜搜:" + Sosobot + "\n\r\n\r";
}