IIS日志分析-如何检索Referer信息
根据这篇 MSDN 文章:
它表示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();
As per this MSDN article:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题与数据绑定器对数据表进行操作并处理列/属性名称的方式有关。
Eval
使用反射,列名称中的括号字符导致此失败(我需要再次回忆一下这一切是如何工作的,已经有一段时间了)。只需将底层
Container.DataItem
转换为它的类型(DataRowView
),然后选择列:这也更快,因为不使用反射慢的。
我还注意到您的“Referer”拼写错误(有两个“r”),所以也要注意这一点。
要使用
Eval()
使其正常工作,您需要做更多的工作。将其更改为
:在数据绑定器
Eval
表达式中,将名称中带有括号的任何列从:更改为:
但我会使用第一种方法,它的侵入性较小且速度更快。
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 (aDataRowView
), then pick out the column: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:To this:
In the data binder
Eval
expression change any column with parentheses in the name from:to:
But I'd go with the first method, it's less intrusive and much faster.