i am creating a Log Parser whereby i Load a log file and Parse it by going through the log file one line at a time. I am using Regular Expression to parse my Files for a specific Pattern. The problem i am having is that in the actual log file there is a tab or space before or in front of the selected line and my data will not match my comparison data.
How do i remove the Tab or white space in front of the line. I tried Trim() and trimStart() that did not work. Here is my sample codes:
Log File Sample pattern:
[ANY__MOD,80,*AUDIT_GROUPS*] [ANY__MOD] audit_groups_category Key1 contains invalid value. Replace dictionary dat file with one having valid Key1 value ANY_MOD.
Regex Pattern i have for this:
string Pattern = @”/.[([.\d\s\w*]),([.\d\s\w*]),([.\d\s\w*])]\s*[([.\d\s\w*])]\s(.)*”;
Problem i am having:”\t[.22.12.,81,*AUDIT_GROUPS*] [.22.12.] audit_groups_category Key1 contains invalid value. Replace dictionary dat file with one having valid Key1 value ANY_MOD.
[.22.12.,81,*AUDIT_GROUPS*] [.22.12.] audit_groups_category Key1 contains invalid value. Replace dictionary dat file with one having valid Key1 value ANY_MOD.
My Sample Code:
private void ValidateUsingRegularExpression(string Pattern, string serviceName)
{
System.Diagnostics.Debugger.Launch();
string line;
int counter = 0;
string ServiceName = Helpers.GetServiceName(serviceName, _integrationType);
string serviceLogPath = Helpers.GetTppInstallDir() + "logs\\" + ServiceName + ".txt";
// string serviceLogPath = @"C:\totalpayment\logs\EngineService.txt";
// If file does not exist
if(!File.Exists(serviceLogPath))
{
throw new ApplicationException("Was unable to find Log file " + serviceLogPath );
}
System.IO.StreamReader file = new System.IO.StreamReader(serviceLogPath);
line = file.ReadLine();
while (line != null)
{
line = file.ReadLine();
if (line == null)
{
return;
}
line = line.Trim();
if (Regex.IsMatch(line, Pattern, RegexOptions.IgnoreCase))
{
line = line.Trim();
line = line.TrimStart();
if (ErrorToValidate == line)
{
_ValidateErrorinLogFile = "Found";
counter = counter + 1;
// counter++;
_TotalLinesFound = counter.ToString();
}
}
}
}
main function that calls
public void ValidateCompareExclusionErrorsInLogFile()
{
// string Pattern = @"\[\d+\,[A-Z_]+\]
// string Pattern = @"\[([\w\*]+),(\d+),([\w\*]+)\]\s*\[([\w\*_]+)\]\s*([\w\\d\s\.]*)";
//string Pattern = \[([\w\*]+),(\d+),([\w\*]+)\]\s*\[([\w\*\_]+)\]\s*([\w\\d\s\.]*);
string Pattern = @"/.*\[([.\d\s\w\*]*),([.\d\s\w\*]*),([.\d\s\\w\*]*)\]\s*\[([.\d\s\w\*]*)\]\s*(.)*";
//ValidateUsingRegularExpression(Pattern, "TPP.EngineService");
ValidateUsingRegularExpression(Pattern, "engine");
ValidateUsingString(ErrorToValidate, "engine");
//These options are here when needed, If you need to setup different suite of test to run same scenario on different engine,
//this can be dynamically done by turning on the following options below.
// ValidateUsingString("field","engine");
}
This works with the data you provided:
And this is what that means: