2

根据这篇 MSDN 文章:

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

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

我正在尝试使用 ASP.NET 中继器控件显示日志信息:

<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' does not contain a property with the name '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中的代码相同

4

1 回答 1

1

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

只需将底层证券Container.DataItem转换为它的类型(a 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")

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

于 2010-03-16T11:43:33.177 回答