Header Ads

Logging messages in C# .Net based Test Automation Framework using log4net

 






Introduction

As the test automation framework grows and number of test cases increases substantially, introducing a logging module becomes necessary for printing all types of log messages. Logging helps in debugging and knowing what and when an event has occurred at runtime. To introduce such informational messages, we can use popular framework like log4net, NLog or SeriLog. Today we look how we can use log4net in C# .Net Selenium Webdriver based test automation project and log in example.log file.


Getting started with log4net:

log4net is tool used by programmers to log information to the output with speed and flexibility. It has hierarchical loggers and can control which log statements to output at different times during the execution flow.

NuGet Command:

Install-Package log4net -Version 2.0.10






Adding log4net Configuration file:


Once log4Net NuGet package is successfully installed, we need to add log4net configuration file to our framework project.

Add Log4net.config file and include below XML configuration in project.

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
view raw Log4net.config hosted with ❤ by GitHub


After Log4net.config file is added in current project with specified format, which includes configuration to specify the type of log for example, append all logs to console output or append all logs to a log file etc... The configuration file also specifies log pattern and at what level of log to be printed.  The example configuration file which we included will log all the log information in the 'example.log' file. 

We need to specify this configuration as default logging configuration for current project executing assembly as shown in the sample project setup file below:


using log4net;
using log4net.Repository;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.IO;
using System.Reflection;
namespace SeleniumTestProject
{
public class UITests
{
IWebDriver webDriver;
//Get Logger for fully qualified name for type of 'UITests'
private static readonly ILog log = LogManager.GetLogger(typeof(UITests));
//Get the default ILoggingRepository
private static readonly ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
[OneTimeSetUp]
public void OnetimeSetup()
{
// Configuring Log4Net
// Valid XML file with Log4Net Configurations
var fileInfo = new FileInfo(@"Log4net.config");
// Configure default logging repository with Log4Net configurations
log4net.Config.XmlConfigurator.Configure(repository, fileInfo);
// Log info
log.Info("OnetimeSetup Configured");
}
[SetUp]
public void Setup()
{
log.Info("Entering Setup");
webDriver = new ChromeDriver();
webDriver.Manage().Window.Maximize();
log.Info("Exiting Setup");
}
[Test]
public void Test1()
{
log.Info("Entering Test1");
webDriver.Navigate().GoToUrl("https://www.google.com/");
Assert.AreEqual("https://www.google.com/", webDriver.Url);
// Debug Log
log.DebugFormat("Verified {0}", webDriver.Url);
log.Info("Exiting Test1");
}
[TearDown]
public void TearDown()
{
log.Info("Entering TearDown");
webDriver.Close();
webDriver.Quit();
log.Info("Exiting TearDown");
}
}
}
view raw UITest1.cs hosted with ❤ by GitHub


After configuring the log4net, one can use it for logging different levels of log messages on different execution paths of the test automation framework to capture meaningful information at runtime.

Different Levels of Logs that can be generated using log4net framework:

log.Info("Information log message");
log.Debug("Debug log message");
log.Warn("Warning log message");
log.Error("Error log message");
log.Fatal("Fatal log message");
view raw LogExample.cs hosted with ❤ by GitHub

Conclusion:

Using a logging framework like log4net is extremely easy to setup and once in place can provide meaningful information for automation execution and help in debugging and troubleshooting the test automation framework. 



No comments

automationglance.com. Powered by Blogger.