我正在尝试解析包含以下信息的日志文件:
2015-03-08 10:30:01 /user849/connect
2015-03-08 10:30:01 /user262/open-level2-price
2015-03-08 10:30:01 /user839/open-detailed-quotes
2015-03-08 10:30:02 /user145/add-technical-drawing
2015-03-08 10:30:02 /user108/connect
2015-03-08 10:30:03 /user850/filter-changed
2015-03-08 10:30:03 /user818/open-level2-price
2015-03-08 10:30:03 /user841/column-width
2015-03-08 10:30:03 /user850/filter-changed
2015-03-08 10:30:04 /user850/connect
2015-03-08 10:30:04 /user420/duration
2015-03-08 10:30:04 /user851/filter-changed
2015-03-08 10:30:04 /user217/duration
2015-03-08 10:30:05 /user82/update-column-properties
2015-03-08 10:30:05 /user809/open-level2-price
2015-03-08 10:30:05 /user382/add-technical-drawing
2015-03-08 10:30:06 /user851/connect
2015-03-08 10:30:07 /user350/add-technical-drawing
2015-03-08 10:30:09 /user849/filter-changed
2015-03-08 10:30:09 /user842/sort
2015-03-08 10:30:09 /user849/open-market-watch
2015-03-08 10:30:10 /user429/interval
2015-03-08 10:30:10 /user218/change-columns
2015-03-08 10:30:11 /user749/connect
2015-03-08 10:30:13 /user759/open-detailed-quotes
2015-03-08 10:30:14 /user753/connect
2015-03-08 10:30:14 /user377/connect
我正在尝试找到3个最常用的操作及其百分比,我想到的是读取文件,使用一些正则表达式解析行,或者将它们填充到数据表然后处理该数据表,但我无法做到。
您能告诉我该做什么,从哪里开始,或者某些代码示例(最好是c#)?
提前致谢!
编辑:
嗯,(我现在已经成功完成了)至于我尝试过的,这是我的代码
string filePath = @"6458.log";
try
{
DataTable logLines = new DataTable("LogLines");
//logLines.Columns.Add(new DataColumn("DateTime", System.Type.GetType("System.DateTime")));
logLines.Columns.Add(new DataColumn("User", typeof(string)));
logLines.Columns.Add(new DataColumn("Operation", typeof(string)));
string[] lines = System.IO.File.ReadAllLines(filePath);
foreach (string line in lines)
{
var cols = line.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
DataRow dr = logLines.NewRow();
//dr["DateTime"] = cols[0] + " " + cols[1];
dr["User"] = cols[2];
dr["Operation"] = cols[3];
logLines.Rows.Add(dr);
}
var query = from row in logLines.AsEnumerable()
group row by row.Field<string>("Operation") into operations
orderby operations.Count() descending
select new
{
Name = operations.Key,
CountOfClients = operations.Count()
};
}
catch (Exception ex)
{
throw(ex) ;
}
请提供代码以提供进一步说明!
再次感谢
答案 0 :(得分:0)
您可以将文件的行添加到List
,然后使用linq获取您想要的数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<data> logs = new List<data>();
var path=Path.Combine(Environment.CurrentDirectory+@"\file.txt");
using (StreamReader sr = new StreamReader(path))
{
string line;
while((line = sr.ReadLine()) != null)
{
var log = Regex.Split(line, " ");
logs.Add(new data { LogDate=DateTime.Parse(log[0]),Operation=log[1]});
}
}
// here you can use linq to get the data you want from logs list
// end of main
}
public class data
{
public DateTime LogDate { get; set; }
public string Operation { get; set; }
}
// end of class
}
}
file.txt
是您要阅读的日志文件
答案 1 :(得分:0)
如果您只想获取操作和呼叫次数,可以使用这段代码。
Dictionary<string, int> items = new Dictionary<string, int>();
foreach(string line in lines)
{
var cols = line.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var operation = cols[2].Trim();
if(items.Keys.Any(x => x.Equals(operation)))
{
items[operation]++;
}
else
{
items[operation] = 1;
}
}
在此之后你有一个字典,其中的动作是键,而调用的数量是值。
如果你想让解析更加抗错误,你可以在不修改逻辑的情况下改变这部分。
如果您想获取所有操作的计数,请使用此功能。
var actionCount = items.Sum(x => x.Value);
如果你想获得例如“连接”动作的百分比,你可以使用它。
var percentage = 100.0 / actionCount * items["connect"];
但是你必须检查这行的字典中是否有任何“连接”条目将失败。您可以查看是否存在密钥,您可以使用
items.ContainsKey("connect");