C# 分析 IIS 日誌(Log)

來源:互聯網
上載者:User
由於最近又要對 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";
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.