IIS日志分析-如何检索Referer信息

发布于 2024-08-24 23:33:30 字数 2253 浏览 8 评论 0原文

根据这篇 MSDN 文章:

W3C 扩展日志文件格式 (IIS 6.0)

它表示cs(Referrer)包含可以从IIS日志文件中读取的REFERER信息。

我尝试使用 ASP.NET Repeater 控件显示日志信息:

<asp:Repeater ID="rptlIISLogEntries" runat="server">
...
...
    <ItemTemplate>
        <tr>
            <td><%# Eval("time")%></td>
            <td><%# Eval("cs(Referrer)")%></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

带有 Eval("cs(Referrer)" 的行引发异常:

DataBinding:“System.Data.DataRowView”不包含名为“cs”的属性。

我的问题是,如何在中继器中显示 REFERER 信息?

解析日志文件并将其绑定到转发器的代码如下:

string theDate =txtDate.Text;
        string FILE_NAME = @"\\" +txtMachine.Text +
           @"\C$\WINNT\System32\LogFiles\" +  
          drpSiteBox.SelectedItem.Text + @"\ex" + theDate + ".log";
        FileStream fs = new FileStream(FILE_NAME, FileMode.Open,
                             FileAccess.Read,FileShare.ReadWrite);
        StreamReader sr = new StreamReader(fs); 
        string strResult = sr.ReadToEnd();
        sr.Close();
        fs.Close();
        sr=null;
        fs=null;

        string[] arLogLines = strResult.Split(Convert.ToChar("\n"));
        dt = new DataTable("log");
        string revisedColmNames=arLogLines[3].Replace("#Fields: ","");
        string[] arColm=revisedColmNames.Split(Convert.ToChar(" "));
for(int j=0;j<arColm.Length;j++)
{   
    dt.Columns.Add(arColm[j]);
    Debug.WriteLine(arColm[j]);
}
for (i =arLogLines.Length-1; i>3;i--)
{  
    // need this because some logs get additional data appended 
    // aren't unhandled exceptions great? The CLR just loves 'em...
    try
    {
        dt.Rows.Add(arLogLines[i].Split(Convert.ToChar(" ")));
    }
    catch {}

}
DataGrid1.DataSource=dt;
DataGrid1.DataBind();

注意:这与 http://www.eggheadcafe.com/articles/20021203.asp

As per this MSDN article:

W3C Extended Log File Format (IIS 6.0)

It says cs(Referrer) contains the REFERER information that can be read from the IIS log files.

I am trying to display the log information using an ASP.NET Repeater control:

<asp:Repeater ID="rptlIISLogEntries" runat="server">
...
...
    <ItemTemplate>
        <tr>
            <td><%# Eval("time")%></td>
            <td><%# Eval("cs(Referrer)")%></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

The line with Eval("cs(Referrer)" throws an exception:

DataBinding:'System.Data.DataRowView' does not contain a property with the name 'cs'.

My question is, how do I display the REFERER information in the repeater?

The code to parse the log file and bind it to the repeater is as follows:

string theDate =txtDate.Text;
        string FILE_NAME = @"\\" +txtMachine.Text +
           @"\C$\WINNT\System32\LogFiles\" +  
          drpSiteBox.SelectedItem.Text + @"\ex" + theDate + ".log";
        FileStream fs = new FileStream(FILE_NAME, FileMode.Open,
                             FileAccess.Read,FileShare.ReadWrite);
        StreamReader sr = new StreamReader(fs); 
        string strResult = sr.ReadToEnd();
        sr.Close();
        fs.Close();
        sr=null;
        fs=null;

        string[] arLogLines = strResult.Split(Convert.ToChar("\n"));
        dt = new DataTable("log");
        string revisedColmNames=arLogLines[3].Replace("#Fields: ","");
        string[] arColm=revisedColmNames.Split(Convert.ToChar(" "));
for(int j=0;j<arColm.Length;j++)
{   
    dt.Columns.Add(arColm[j]);
    Debug.WriteLine(arColm[j]);
}
for (i =arLogLines.Length-1; i>3;i--)
{  
    // need this because some logs get additional data appended 
    // aren't unhandled exceptions great? The CLR just loves 'em...
    try
    {
        dt.Rows.Add(arLogLines[i].Split(Convert.ToChar(" ")));
    }
    catch {}

}
DataGrid1.DataSource=dt;
DataGrid1.DataBind();

Note: this is the same code as in http://www.eggheadcafe.com/articles/20021203.asp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-08-31 23:33:30

这里的问题与数据绑定器对数据表进行操作并处理列/属性名称的方式有关。 Eval 使用反射,列名称中的括号字符导致此失败(我需要再次回忆一下这一切是如何工作的,已经有一段时间了)。

只需将底层 Container.DataItem 转换为它的类型(DataRowView),然后选择列:

<%# ((System.Data.DataRowView)Container.DataItem)["cs(Referer)"]%>

这也更快,因为不使用反射慢的。

我还注意到您的“Referer”拼写错误(有两个“r”),所以也要注意这一点。

要使用 Eval() 使其正常工作,您需要做更多的工作。将其更改

for(int j=0;j<arColm.Length;j++)
{   
    dt.Columns.Add(arColm[j]);
    Debug.WriteLine(arColm[j]);
}

for (int j = 0; j < arColm.Length; j++)
{
    string colName = arColm[j].Replace("(", "_").Replace(")", "");
    dt.Columns.Add(colName);
    Debug.WriteLine(colName);
}

:在数据绑定器 Eval 表达式中,将名称中带有括号的任何列从:更改

Eval("cs(Referer)")

为:

Eval("cs_Referer")

但我会使用第一种方法,它的侵入性较小且速度更快。

The problem here is to do with the way that the data binder operates over the data table and handles column/property names. Eval uses reflection and the parentheses characters in the column names are causing this to fail (I need to jog my memory about how all this works again, it's been a while).

Just cast the underlying Container.DataItem to the type that it is (a DataRowView), then pick out the column:

<%# ((System.Data.DataRowView)Container.DataItem)["cs(Referer)"]%>

This is also faster because reflection isn't used which is slow.

Also I notice that you'd spelled 'Referer' incorrectly (you have two 'r's), so watch out for that as well.

To get this to work using Eval() instead you need to do a bit more work. Change this:

for(int j=0;j<arColm.Length;j++)
{   
    dt.Columns.Add(arColm[j]);
    Debug.WriteLine(arColm[j]);
}

To this:

for (int j = 0; j < arColm.Length; j++)
{
    string colName = arColm[j].Replace("(", "_").Replace(")", "");
    dt.Columns.Add(colName);
    Debug.WriteLine(colName);
}

In the data binder Eval expression change any column with parentheses in the name from:

Eval("cs(Referer)")

to:

Eval("cs_Referer")

But I'd go with the first method, it's less intrusive and much faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文