C# 如何匹配带关键字和不带关键字的多行日志条目(regex)

C# 如何匹配带关键字和不带关键字的多行日志条目(regex),c#,.net,regex,multiline,C#,.net,Regex,Multiline,我的日志文件如下所示: *** DEBUG 2015-03-18 12:11:46.678 [sendTaskResponse] ID2200* Creating object GUID=6b7582ba-eb8b-4084-b726-26901827f150 *** ERROR 2015-03-18 12:11:46.912 [ 23] ID2543 Details: System.IO.IOException: Incorrect WPDU:

我的日志文件如下所示:

 *** DEBUG 2015-03-18 12:11:46.678 [sendTaskResponse] ID2200*     
 Creating object GUID=6b7582ba-eb8b-4084-b726-26901827f150

 *** ERROR 2015-03-18 12:11:46.912 [        23] ID2543      
 Details:
 System.IO.IOException: Incorrect WPDU: 22354
    w Devices.DlmsConnection.Connect(DeviceE device, AGPRSKeysAssocEnum user) in d:\current\csharp\AProject\ADriver\Devices\DeviceE.cs:row 1921
    w Devices.Device.Connect(AGPRSKeysAssocEnum user) in d:\A\current\csharp\AProject\ADriver\Devices\DeviceE.cs:row 522
    w A.Devices.DeviceE.InitTask(DelegatedObjectTask thisTask) in d:\A\current\csharp\AProject\ADriver\Devices\DeviceE.cs:row 252

 *** DEBUG 2015-03-18 12:11:46.929 [sendStatus] ID2200*     
 Sending XML N.F6.Data.F6ObjectStatusData

 <?xml version="1.0" encoding="utf-16"?>
 <F6ObjectStatusData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <sourceId>2200</sourceId>
   <status>4</status>
   <statusText> </statusText>
   <timestamp>2015-03-18T11:11:46.6237699Z</timestamp>
 </F6ObjectStatusData>
 <?xml version="1.0" encoding="utf-16"?>
 <F6ObjectStatusData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <sourceId>2202</sourceId>
   <status>4</status>
   <statusText>Device ready</statusText>
   <timestamp>2015-03-18T12:11:21.7213456+01:00</timestamp>
 </F6ObjectStatusData>

 *** DEBUG 2015-03-18 12:11:47.263 [sendStatus] ID2200*     
 Posted Objects Count=2
调试2015-03-18 12:11:46.678[sendTaskResponse]ID2200* 创建对象GUID=6b7582ba-eb8b-4084-b726-26901827f150 ***错误2015-03-18 12:11:46.912[23]ID2543 细节: System.IO.IOException:不正确的WPDU:22354 w Devices.DlmsConnection.Connect(DeviceE设备,AGPRSKeyassocenum用户)位于d:\current\csharp\AProject\ADriver\Devices\DeviceE.cs:第1921行 在d:\A\current\csharp\AProject\ADriver\Devices\Devices.Connect(agprskysassocenum用户)中的w Devices.Device.Connect:第522行 d:\A\current\csharp\AProject\ADriver\Devices\DeviceE.cs中的w A.Devices.DeviceE.InitTask(DelegatedObjectTask thisttask):第252行 ***调试2015-03-18 12:11:46.929[发送状态]ID2200* 发送XML N.F6.Data.F6ObjectStatusData 2200 4. 2015-03-18T11:11:46.6237699Z 2202 4. 设备就绪 2015-03-18T12:11:21.7213456+01:00 ***调试2015-03-18 12:11:47.263[发送状态]ID2200* 已发布对象计数=2
1) 我需要获取这4个日志条目中的每一个
因此,从以***
接下来的每一行都不是以***
2) 我需要包含一些关键字的日志条目(例如单词“agprskeyassocenum”-第二个日志条目)
当然,它必须用正则表达式模式来完成。

使用关键字搜索条目首先拆分日志,然后对每个日志应用类似的正则表达式,例如:

string text = "Your log text";
IEnumerable<string> splittedLog = GetEntriesForLog(text);
string strRegex = @".*?AGPRSKeysAssocEnum.*|\z";
Regex myRegex = new Regex(strRegex, RegexOptions.ExplicitCapture | RegexOptions.Singleline);

var entries = splittedLog.Where(entry => myRegex.IsMatch(entry)).ToList();
string text=“您的日志文本”;
IEnumerable splittedLog=GetEntriesForLog(文本);
字符串stregex=@“*?AGPRSKeysAssocEnum.*\z”;
Regex myRegex=新的Regex(strRegex,RegexOptions.ExplicitCapture | RegexOptions.Singleline);
var entries=splittedLog.Where(entry=>myRegex.IsMatch(entry)).ToList();

您尝试过哪些不起作用的方法?同时,请在此处查找有关多行正则表达式选项的信息。注释不用于扩展讨论;这段对话已经结束。
string text = "Your log text";
IEnumerable<string> splittedLog = GetEntriesForLog(text);
string strRegex = @".*?AGPRSKeysAssocEnum.*|\z";
Regex myRegex = new Regex(strRegex, RegexOptions.ExplicitCapture | RegexOptions.Singleline);

var entries = splittedLog.Where(entry => myRegex.IsMatch(entry)).ToList();