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)?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With