I am trying to verify that certain errors are logged in log4net file and how many times.
I am working on following code which will read one value and assert if it is right, but
I want to see how many times it appeared in the log file than assert it if it is true.
private string logfile;
[SetUp]
public void SetUp()
{
logfile = Path.Combine(
Environment.GetEnvironmentVariable("ALLUSERSPROFILE"),
"test.log");
if (File.Exists(logfile))
File.Delete(logfile);
XmlConfigurator.Configure();
}
[Test] public void GivenLog4NetFileAppender_WhenLogInfoStringWithLog4Net_ThenWritesToDisk()
{
ILog log = LogManager.GetLogger(typeof (LoggingIntegrationTests));
log.Info("Error 2");
LogManager.Shutdown();
Assert.That(File.ReadAllText(logfile),
Is.StringContaining("Error 2"));
}
So in above code if Error 2 is logged 3 times than I want to assert that it appeared 3 times.
Thanks for you help
You could use a Regex to count the number of times a given string occurs within another string: