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.Log4NetReader
8
{
9
public partial class Log4NetShow : Form
10
{
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
try
37
{
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.Log4NetReader
5
{
6
public static class RegexUtil
7
{
8
public static string Replace(string inputData, string pattern, string replaceData)
9
{
10
try
11
{
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
try
25
{
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.Log4NetReader
5
{
6
public class FileReader
7
{
8
/// <summary>
9
/// 读取文本文件
10
/// </summary>
11
/// <param name="filePath"></param>
12
public static string ReadFlie(string filePath)
13
{
14
try
15
{
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决