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)
完整的代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Data;
3
using System.Windows.Forms;
4
using System.IO;
5
using System.Text.RegularExpressions;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace MorningStar.Blade.Log4NetReader
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public partial class Log4NetShow : Form
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
public Log4NetShow()
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
InitializeComponent();
21
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
private string GetTextFromFile(string filePath)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
string tempFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,String.Format("temp{0}.txt",System.DateTime.Now.ToBinary()));
27
File.Copy(filePath,tempFile);
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
string text = FileReader.ReadFlie(tempFile);
30
File.Delete(tempFile);
31
return text;
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
private void btnOpenFile_Click(object sender, EventArgs e)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
try
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
using (OpenFileDialog ofd = new OpenFileDialog())
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
if (ofd.ShowDialog() == DialogResult.OK)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
txtFileName.Text = ofd.FileName;
43
string textFromFile = GetTextFromFile(ofd.FileName);
44
if (!String.IsNullOrEmpty(textFromFile))
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
MessageBox.Show(ex.Message, "Warn", MessageBoxButtons.OK, MessageBoxIcon.Warning);
57
}
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
private DataTable GetLogData(MatchCollection collection)
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
string date = "";
64
string time = "";
65
string thread = "";
66
string level = "";
67
string message = "";
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
foreach (Match match in collection)
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
dr["Message"] = message;
91
logTable.Rows.Add(dr);
92
}
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
return logTable;
95
}
96
}
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
RegexUtil的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Text.RegularExpressions;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace MorningStar.Blade.Log4NetReader
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public static class RegexUtil
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
public static string Replace(string inputData, string pattern, string replaceData)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
try
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
Regex match = new Regex(pattern);
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
return match.Replace(inputData, replaceData);
15
}
16
catch (Exception ex)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
return "";
19
}
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
public static string GetGroupValue(string inputData, string pattern, string groupName)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
try
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
Regex match = new Regex(pattern);
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
return match.Match(inputData).Groups[groupName].Value;
29
}
30
catch (Exception ex)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return "";
33
}
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
public static MatchCollection GetMatchCollection(string inputData, string pattern)
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
39
return re.Matches(inputData);
40
}
41
}
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FileReader的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.IO;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace MorningStar.Blade.Log4NetReader
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public class FileReader
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
9
/// 读取文本文件
10
/// </summary>
11
/// <param name="filePath"></param>
12
public static string ReadFlie(string filePath)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
try
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
string str = "";
17
using (StreamReader sr = new StreamReader(filePath))
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
str = sr.ReadToEnd();
20
sr.Close();
21
}
22
return str;
23
}
24
catch (Exception ex)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
throw ex;
27
}
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
}
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)