I'm working on a .net core 2.1 application and trying to incorporate log4net to write logs to files. I can get it to work but can't figure out how to write to different environments (i.e. test/production) where the logs will be in different locations.
log4net.config
<log4net>
<root>
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level - %message%newline" />
</layout>
<threshold value="Info" />
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="C:\logs\TestLocation\MyLog.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="100MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level - %message%newline" />
</layout>
<threshold value="Info" />
</appender>
</log4net>
my startup.cs configure method is:
loggerFactory.AddLog4Net();
I inject the logger into my controller:
public MyController(ILogger<MyController> logger)
{
_logger = logger;
}
and use it in my controller:
_logger.LogInformation("My logger worked!!!!!!!!!!!");
This all works fine. I was hoping I could just add a new log4net.config file and just name it log4net.Production.config and the application would use the different config file depending on the environment that it's in like appnsettings.json. Instead it just uses my default log4net.config file.
Is it possible to have multiple log4net.config files for each environment like appsettings.json?
Log4NetCore.PropertyOverrides
. – Putrid