<%@ Page EnableSessionState="False" Language="c#" %>
<%@Import Namespace="System.IO" %>
<%@Import Namespace="System.Collections" %>
<%
string logfilesRoot = "C:\\WINNT\\system32\\Logfiles\\W3SVC1\\";
//格式说明:ex020428代表2002年4月28日的IIS日志文件,如果想查看2002年5月1日的则填入:ex020501,如此类推.....
string fName = "ex020428";
string groupCol, sumCol;
int groupNr = -1;
int sumNr = -1;
int n = 0;
string aLine, s;
string[] cols;
bool isUsefullPart = false;
int cntSkippedLines = 0;
int cntDataLines = 0;
int addValue;
Hashtable data = new Hashtable();
if (Request.QueryString["fName"] != null)
fName = Request.QueryString["fName"];
groupCol = Request.QueryString["groupCol"];
sumCol = Request.QueryString["sumCol"];
// more fields: date cs-uri-query
// a list with usable fields of the logfile
string opts = "<OPTION>time</OPTION>"
+ "<OPTION>c-ip</OPTION>"
+ "<OPTION>cs-username</OPTION>"
+ "<OPTION>cs-method</OPTION>"
+ "<OPTION>cs-uri-stem</OPTION>"
+ "<OPTION>sc-status</OPTION>"
+ "<OPTION>sc-bytes</OPTION>"
+ "<OPTION>cs-bytes</OPTION>"
+ "<OPTION>time-taken</OPTION>"
+ "<OPTION>cs(User-Agent)</OPTION>";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
<head>
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<title>IIS Logfile Analyzer .NET</title>
<link href="../style/P2plus.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../WebObjects/utils.js"></script>
<style>
BODY, TD, INPUT, TEXTAREA, SELECT
{FONT-FAMILY: Arial,Verdana; FONT-SIZE: 12px; COLOR: black; }
BODY {margin:8px; BACKGROUND-COLOR: #dbe7f3;}
TABLE { EMPTY-CELLS: show; BORDER-COLLAPSE:collapse;}
HR {COLOR:#395d9a; }
#resultTab {BORDER: #395d9a 1px solid }
#resultTab TD {BORDER: #395d9a 1px solid; BACKGROUND-COLOR: white; }
#resultTab THEAD TD { BACKGROUND-COLOR: #aabee3; }
</style>
</head>
<body>
<h2 align="center">IIS Logfile Analyzer .NET</h2>
<h3>Start new Report:</h3>
<form method=GET id=form1 name=form1>
<table>
<tr><td>Group by:</td><td><SELECT name=groupCol><%=opts%></SELECT></td></tr>
<tr><td>Sum of:</td><td><SELECT name=sumCol><OPTION>(hits)</OPTION><%=opts%></SELECT></td></tr>
<tr><td>Logfile:</td><td><input name=fName value="<%=fName%>"></td></tr>
<tr><td align=right colspan=2><input type='submit' value='Submit'></td></tr>
</table>
<hr>
</form>
<%
if (groupCol != null) {
// ouput links with avaliable Group-Columns
TextReader aFile = File.OpenText(logfilesRoot + fName + ".log");
// analyze Logfile
Response.Write("<h3 id=bar>Processing...</h3>");
Response.Flush();
DateTime d1 = DateTime.Now;
aLine = aFile.ReadLine();
while (aLine != null) {
cols = aLine.Split(' ');
if (cols[0][0] == '#') {
// this line contains metadata
if (cols[0] == "#Fields:") {
groupNr = -1;
sumNr = -1;
for (n = 1; n < cols.Length; n++) {
if (cols[n] == groupCol) groupNr = n-1;
if (cols[n] == sumCol) sumNr = n-1;
} // for
isUsefullPart = (groupNr >= 0);
} // if
} else if (! isUsefullPart) {
// skip this line
cntSkippedLines++;
} else {
// this line contains data
cntDataLines++;
if (sumNr < 0) {
addValue = 1;
} else {
s = cols[sumNr];
addValue = int.Parse(s);
} s = cols[groupNr];
if (groupCol == "time") s = s.Substring(0, 5); if (data.ContainsKey(s))
n = addValue + (int)data[s];
else
n = addValue;
data[s] = n;
} // if
aLine = aFile.ReadLine();
} // while
aFile.Close();
DateTime d2 = DateTime.Now;
Response.Write("<table id=resultTab><thead><tr><td>" + groupCol + "<td>Sum of " + sumCol + "</tr></thead>");
Response.Write("<tbody>");
foreach (string e in data.Keys) {
Response.Write("<tr><td>" + e + "</td><td align=right>" + data[e] + "</td></tr>");
} // for
Response.Write("</tbody></table>");
%>
<hr>
<h3>Summary:</h3>
<table>
<tr><td>analyzed Lines<td><%=cntDataLines%></tr>
<tr><td>skipped Lines<td><%=cntSkippedLines%></tr>
<tr><td>Time<td><%=(d2.Ticks - d1.Ticks)/10000%> msec.</tr>
</table>
<SCRIPT LANGUAGE=javascript>
bar.innerText = "Result:";
</SCRIPT>
<% } // if %>
</body>
</html>
<%@Import Namespace="System.IO" %>
<%@Import Namespace="System.Collections" %>
<%
string logfilesRoot = "C:\\WINNT\\system32\\Logfiles\\W3SVC1\\";
//格式说明:ex020428代表2002年4月28日的IIS日志文件,如果想查看2002年5月1日的则填入:ex020501,如此类推.....
string fName = "ex020428";
string groupCol, sumCol;
int groupNr = -1;
int sumNr = -1;
int n = 0;
string aLine, s;
string[] cols;
bool isUsefullPart = false;
int cntSkippedLines = 0;
int cntDataLines = 0;
int addValue;
Hashtable data = new Hashtable();
if (Request.QueryString["fName"] != null)
fName = Request.QueryString["fName"];
groupCol = Request.QueryString["groupCol"];
sumCol = Request.QueryString["sumCol"];
// more fields: date cs-uri-query
// a list with usable fields of the logfile
string opts = "<OPTION>time</OPTION>"
+ "<OPTION>c-ip</OPTION>"
+ "<OPTION>cs-username</OPTION>"
+ "<OPTION>cs-method</OPTION>"
+ "<OPTION>cs-uri-stem</OPTION>"
+ "<OPTION>sc-status</OPTION>"
+ "<OPTION>sc-bytes</OPTION>"
+ "<OPTION>cs-bytes</OPTION>"
+ "<OPTION>time-taken</OPTION>"
+ "<OPTION>cs(User-Agent)</OPTION>";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
<head>
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<title>IIS Logfile Analyzer .NET</title>
<link href="../style/P2plus.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../WebObjects/utils.js"></script>
<style>
BODY, TD, INPUT, TEXTAREA, SELECT
{FONT-FAMILY: Arial,Verdana; FONT-SIZE: 12px; COLOR: black; }
BODY {margin:8px; BACKGROUND-COLOR: #dbe7f3;}
TABLE { EMPTY-CELLS: show; BORDER-COLLAPSE:collapse;}
HR {COLOR:#395d9a; }
#resultTab {BORDER: #395d9a 1px solid }
#resultTab TD {BORDER: #395d9a 1px solid; BACKGROUND-COLOR: white; }
#resultTab THEAD TD { BACKGROUND-COLOR: #aabee3; }
</style>
</head>
<body>
<h2 align="center">IIS Logfile Analyzer .NET</h2>
<h3>Start new Report:</h3>
<form method=GET id=form1 name=form1>
<table>
<tr><td>Group by:</td><td><SELECT name=groupCol><%=opts%></SELECT></td></tr>
<tr><td>Sum of:</td><td><SELECT name=sumCol><OPTION>(hits)</OPTION><%=opts%></SELECT></td></tr>
<tr><td>Logfile:</td><td><input name=fName value="<%=fName%>"></td></tr>
<tr><td align=right colspan=2><input type='submit' value='Submit'></td></tr>
</table>
<hr>
</form>
<%
if (groupCol != null) {
// ouput links with avaliable Group-Columns
TextReader aFile = File.OpenText(logfilesRoot + fName + ".log");
// analyze Logfile
Response.Write("<h3 id=bar>Processing...</h3>");
Response.Flush();
DateTime d1 = DateTime.Now;
aLine = aFile.ReadLine();
while (aLine != null) {
cols = aLine.Split(' ');
if (cols[0][0] == '#') {
// this line contains metadata
if (cols[0] == "#Fields:") {
groupNr = -1;
sumNr = -1;
for (n = 1; n < cols.Length; n++) {
if (cols[n] == groupCol) groupNr = n-1;
if (cols[n] == sumCol) sumNr = n-1;
} // for
isUsefullPart = (groupNr >= 0);
} // if
} else if (! isUsefullPart) {
// skip this line
cntSkippedLines++;
} else {
// this line contains data
cntDataLines++;
if (sumNr < 0) {
addValue = 1;
} else {
s = cols[sumNr];
addValue = int.Parse(s);
} s = cols[groupNr];
if (groupCol == "time") s = s.Substring(0, 5); if (data.ContainsKey(s))
n = addValue + (int)data[s];
else
n = addValue;
data[s] = n;
} // if
aLine = aFile.ReadLine();
} // while
aFile.Close();
DateTime d2 = DateTime.Now;
Response.Write("<table id=resultTab><thead><tr><td>" + groupCol + "<td>Sum of " + sumCol + "</tr></thead>");
Response.Write("<tbody>");
foreach (string e in data.Keys) {
Response.Write("<tr><td>" + e + "</td><td align=right>" + data[e] + "</td></tr>");
} // for
Response.Write("</tbody></table>");
%>
<hr>
<h3>Summary:</h3>
<table>
<tr><td>analyzed Lines<td><%=cntDataLines%></tr>
<tr><td>skipped Lines<td><%=cntSkippedLines%></tr>
<tr><td>Time<td><%=(d2.Ticks - d1.Ticks)/10000%> msec.</tr>
</table>
<SCRIPT LANGUAGE=javascript>
bar.innerText = "Result:";
</SCRIPT>
<% } // if %>
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货