解析,log4日志的输出格式 【转载】
Log4Net的Pattern:
%date [%thread] %-5level- %message%newline
正则表达式的格式:
(?<Date>[0-9]{4}-[0-9]{2}-[0-9]{2}) (?<Time>[0-9]{2}:[0-9]{2}:[0-9]{2})\,[0-9]{3} \[(?<Thread>[\s\S]*?)\] (?<Level>[DEBUG|WARN |FATAL|ERROR|INFO ]*?)- (?<message>[\s\S]*?\n)
完整的代码如下:
1
using System;2
using System.Data;3
using System.Windows.Forms;4
using System.IO;5
using System.Text.RegularExpressions;6

7
namespace MorningStar.Blade.Log4NetReader8
{9
public partial class Log4NetShow : Form10
{11
private const string PATTERN = @"(?<Date>[0-9]{4}-[0-9]{2}-[0-9]{2}) (?<Time>[0-9]{2}:[0-9]{2}:[0-9]{2})\,[0-9]{3} \[(?<Thread>[\s\S]*?)\] (?<Level>[DEBUG|WARN |FATAL|ERROR|INFO ]*?)- (?<message>[\s\S]*?\n)";12
private const string DATEPATTERN = "Date";13
private const string TIMEPATTERN = "Time";14
private const string THREADPATTERN = "Thread";15
private const string LEVELPATTERN = "Level";16
private const string MESSAGEPATTERN = "message";17

18
public Log4NetShow()19
{20
InitializeComponent();21
22
}23

24
private string GetTextFromFile(string filePath)25
{26
string tempFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,String.Format("temp{0}.txt",System.DateTime.Now.ToBinary()));27
File.Copy(filePath,tempFile);28

29
string text = FileReader.ReadFlie(tempFile);30
File.Delete(tempFile);31
return text;32
}33

34
private void btnOpenFile_Click(object sender, EventArgs e)35
{36
try37
{38
using (OpenFileDialog ofd = new OpenFileDialog())39
{40
if (ofd.ShowDialog() == DialogResult.OK)41
{42
txtFileName.Text = ofd.FileName;43
string textFromFile = GetTextFromFile(ofd.FileName);44
if (!String.IsNullOrEmpty(textFromFile))45
{46
MatchCollection collection = RegexUtil.GetMatchCollection(textFromFile, PATTERN);47
DataTable logTable = GetLogData(collection);48
if (logTable != null && logTable.Rows.Count > 0)49
gdLog.DataSource = logTable;50
}51
}52
}53
}54
catch (Exception ex)55
{56
MessageBox.Show(ex.Message, "Warn", MessageBoxButtons.OK, MessageBoxIcon.Warning);57
}58

59
}60

61
private DataTable GetLogData(MatchCollection collection)62
{63
string date = "";64
string time = "";65
string thread = "";66
string level = "";67
string message = "";68

69
DataTable logTable = new DataTable();70
logTable.Columns.Add(new DataColumn("LogDate", typeof(DateTime)));71
logTable.Columns.Add(new DataColumn("ThreadName", typeof(string)));72
logTable.Columns.Add(new DataColumn("Level", typeof(string)));73
logTable.Columns.Add(new DataColumn("Message", typeof(string)));74

75
foreach (Match match in collection)76
{77
date = match.Groups[DATEPATTERN].Value;78
time = match.Groups[TIMEPATTERN].Value;79
thread = match.Groups[THREADPATTERN].Value;80
level = match.Groups[LEVELPATTERN].Value;81
message = match.Groups[MESSAGEPATTERN].Value;82

83
DataRow dr = logTable.NewRow();84
DateTime temp;85
DateTime.TryParse(String.Format("{0} {1}", date, time), out temp);86
dr["LogDate"] = temp;87
dr["ThreadName"] = thread;88
dr["Level"] = level;89

90
dr["Message"] = message;91
logTable.Rows.Add(dr);92
}93

94
return logTable;95
}96
}97
}98

RegexUtil的代码:
1
using System;2
using System.Text.RegularExpressions;3

4
namespace MorningStar.Blade.Log4NetReader5
{6
public static class RegexUtil7
{8
public static string Replace(string inputData, string pattern, string replaceData)9
{10
try11
{12
Regex match = new Regex(pattern);13

14
return match.Replace(inputData, replaceData);15
}16
catch (Exception ex)17
{18
return "";19
}20
}21

22
public static string GetGroupValue(string inputData, string pattern, string groupName)23
{24
try25
{26
Regex match = new Regex(pattern);27

28
return match.Match(inputData).Groups[groupName].Value;29
}30
catch (Exception ex)31
{32
return "";33
}34
}35

36
public static MatchCollection GetMatchCollection(string inputData, string pattern)37
{38
Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);39
return re.Matches(inputData);40
}41
}42
}43

FileReader的代码:
1
using System;2
using System.IO;3

4
namespace MorningStar.Blade.Log4NetReader5
{6
public class FileReader7
{8
/// <summary>9
/// 读取文本文件10
/// </summary>11
/// <param name="filePath"></param>12
public static string ReadFlie(string filePath)13
{14
try15
{16
string str = "";17
using (StreamReader sr = new StreamReader(filePath))18
{19
str = sr.ReadToEnd();20
sr.Close();21
}22
return str;23
}24
catch (Exception ex)25
{26
throw ex;27
}28
} 29

30
}31
}32

浙公网安备 33010602011771号