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:
<?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> |
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"); | |
} | |
} | |
} |
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"); |
Post a Comment