Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring log4net in unit tests to log to console and display proper date and time

I am running some unit tests, and I would like log4net to log everything to the Console (just for inspection). Following the manual, I used:

BasicConfigurator.Configure();

However, this sets up the logging pattern to %-4timestamp [%thread] %-5level %logger %ndc - %message%newline. How can I change it so that instead of the timestamp I get the usual date and time (up to miliseconds)?

like image 349
Grzenio Avatar asked Jan 03 '14 08:01

Grzenio


People also ask

What are the main components of log4net briefly describe how loggers work in log4net?

Log4net has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Is log4net dependent on log4j?

Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.


1 Answers

You can get ConsoleAppender from logger repository and change it's layout:

BasicConfigurator.Configure();
var appender = LogManager.GetRepository()
                         .GetAppenders()
                         .OfType<ConsoleAppender>()
                         .First();

appender.Layout = new PatternLayout("%d %-5level %logger - %m%n"); // set pattern
ILog logger = LogManager.GetLogger(logger_name); // obtain logger

But configuration via config file is preferred way, because you can change settings without recompiling your tests.

like image 188
Sergey Berezovskiy Avatar answered Sep 22 '22 04:09

Sergey Berezovskiy