改进格式
因此,我之前使用的方法都没有帮助我对日志文件数据进行聚类:(
现在我将尝试索引方法..我需要根据URL字段中显示的关键字索引每个日志文件条目。
例子:
192.162.1.4 [3/May/2009 00:34:45] "GET /books/casual/4534.pdf" 200 454353 "http://ljdhjg.com" "Mozillablahblah"<br/>
190.100.1.4 [3/May/2009 00:37:45] "GET /resources/help.pdf" 200 4353 "http://ljdhjg.com" "Mozillablahblah"<br/>
192.162.1.4 [3/May/2009 00:40:45] "GET /books/serious/44.pdf" 200 234353 "http://ljdhjg.com" "Mozillablahblah"<br/>
....我还有数千个这样的参赛作品..
现在需要为所有"books"
分配一个数字...... 1(比如说)..然后,"resources"
需要分配2 ..我将如何在C#中完成此操作?我的意思是,我知道逻辑......
使用每行文件提取keyword..assign number..compare关键字数组。如果匹配,则分配。但是因为我是C#的新手,我真的不知道如何编写上面提到的逻辑。 So..help?
答案 0 :(得分:0)
您可以尝试使用此adhoc方法进行分配(我假设这意味着将索引添加到日志条目前面),
/// <summary>
/// Gets the indexed entry.
/// </summary>
/// <param name="entry">The log entry.</param>
/// <returns>The log entry prefixed with the index.</returns>
private string GetIndexedEntry(string entry)
{
string keyword = GetKeyword(entry);
switch (keyword)
{
case "books":
entry = "1 : " + entry;
break;
case "resources":
entry = "2 : " + entry;
break;
}
// Alternative code (using dictionary)
// entry = keywordIndexDictionary[keyword] + " : " + entry;
return entry;
}
/// <summary>
/// Gets the keyword.
/// </summary>
/// <param name="entry">The log entry.</param>
/// <returns>The keyword for the specified log entry.</returns>
private string GetKeyword(string entry)
{
int index = entry.IndexOf("\"GET");
entry = entry.Substring(index + ("\"GET").Length);
index = entry.IndexOf('"');
entry = entry.Substring(0, index).Trim();
return entry.Split('/')[1];
}
// Alternative approach
/// <summary>
/// Stores the keyword-index pair
/// </summary>
private Dictionary<string, int> keywordIndexDictionary;
/// <summary>
/// Builds the dictionary.
/// </summary>
private void BuildDictionary()
{
// Build the dictionary manually
// or alternatively read from an settings file and then build the dictionary
keywordIndexDictionary.Add("books", 1);
keywordIndexDictionary.Add("resources", 2);
}
对GetIndexedEntry()
的调用看起来像,
string indexedLogEntry = GetIndexedEntry(logEntry);
其中logEntry
是表示日志文件中每个条目的字符串。
logEntry
192.162.1.4 [3/May/2009 00:34:45] "GET /books/casual/4534.pdf" 200 454353 "http://ljdhjg.com" "Mozillablahblah"
indexedLogEntry
将是
1 : 192.162.1.4 [3/May/2009 00:34:45] "GET /books/casual/4534.pdf" 200 454353 "http://ljdhjg.com" "Mozillablahblah"
如果使用正则表达式,则可以采用更优雅的方法。