所以我需要一些帮助。 我有一个服务器日志,需要从日志中过滤出错误代码(404)。 到目前为止,我已经从日志中删除了错误代码,但仍然显示了我不需要的成功连接代码(200)。 我是C#的新手,所以需要任何帮助。
这就是我所拥有的:
private void btnOpen_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
StreamReader streamreader = new StreamReader(filename);
string value = filename;
while (!streamreader.EndOfStream)
{
string data = bestand.ReadLine();
// Split the data to keep only the error codes
string[] errorcodeArray = data.Split('"');
string trim = Regex.Replace(errorcodeArray[2], @"", "");
// Trim to keep only the 3 figure codes
trim = trim.Substring(0, trim.IndexOf(" ") + 5);
txtLog.Text += Environment.NewLine + data;
txtError.Text += Environment.NewLine + trim;
// Couldn't get the 404's out of this.
}
streamreader.Close();
日志样本:
109.169.248.247 - - [12/Dec/2015:18:25:11 +0100] "GET /administrator/ HTTP/1.1" 200 4263 "-" "Mozilla/5.0 (Windows NT 6.0; rv:34.0) Gecko/20100101 Firefox/34.0" "-"
109.169.248.247 - - [12/Dec/2015:18:25:11 +0100] "POST /administrator/index.php HTTP/1.1" 200 4494 "almhuette-raith.at/administrator" "Mozilla/5.0 (Windows NT 6.0; rv:34.0) Gecko/20100101 Firefox/34.0" "-"
46.72.177.4 - - [12/Dec/2015:18:31:08 +0100] "GET /administrator/ HTTP/1.1" 200 4263 "-" "Mozilla/5.0 (Windows NT 6.0; rv:34.0) Gecko/20100101 Firefox/34.0" "-"
答案 0 :(得分:0)
单线:
var lines = File.ReadAllLines(/*path*/);
var result = lines.Select(x=> Regex.Replace(x, @"HTTP/1.1"" \d+ ", @"HTTP/1.1"" "));
它将过滤所有代码。 仅需200和404:
var result = lines.Select(x=> Regex.Replace(x, @"HTTP/1.1"" (200|404) ", @"HTTP/1.1"" "));
答案 1 :(得分:0)
我相信您只需要“ 404”代码,并且在示例中不显示任何这些代码。如果它们是相同的格式,则应该起作用:
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
var rows = File.ReadAllLines(filename);
var results = rows.Where(r => r.Split('"')[2].Trim().StartsWith("404"));
如果日志文件很大,并且您不想一次全部读取它,则应该在循环中进行测试。这是一个如何循环执行的完整示例:
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
string data;
//using a string builder to concat strings is much more efficient
StringBuilder sbLog = new StringBuilder();
StringBuilder sbError = new StringBuilder();
using (StreamReader file = new StreamReader(filename))
{
while ((data = file.ReadLine()) != null)
{
if (data.Split('"')[2].Trim().StartsWith("404"))
{
sbLog.Append(data + Environment.NewLine);
sbError.Append(data.Split('"')[2].Trim().Substring(0, 3) + Environment.NewLine);
}
}
}
txtLog.Text = sbLog.ToString();
txtError.Text += sbError.ToString();