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.



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:




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:


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.