Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 2.1 log4net with multip environments

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?

like image 523
Joel Avatar asked Jan 28 '19 20:01

Joel


Video Answer


2 Answers

You have the option to use the advanced custom configuration from your appsettings.{environment}.json.

Overriding the value of the Log4NetConfigFileName property, you will be able to indicate different log4net.config files for each environment.

"Log4NetCore": {
    "Log4NetConfigFileName": "log4net.production.config"
}

In order to get this custom configuration in place, you should replace the AddLog4Net() call by the equivalent call to the AddLog4Net(Log4NetProviderOptions) method.

var loggingOptions = this.Configuration.GetSection("Log4NetCore")
                                       .Get<Log4NetProviderOptions>();
loggerFactory.AddLog4Net(loggingOptions);

As the dynamic appsettings.json load by environment is already configured on your project, the specific configuration for your production environment should be taken automatically.

Hope this helps.

like image 80
HuorSwords Avatar answered Nov 15 '22 14:11

HuorSwords


On your Startup.cs, you can configure it like this:

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
    FileInfo fInfo;

    if (File.Exists(Path.Combine(env.ContentRootPath, $"log4net.{env.EnvironmentName}.xml")))
        fInfo = env.GetLog4NetFile($"log4net.{env.EnvironmentName}.xml");
    else
        fInfo = env.GetLog4NetFile($"log4net.xml");

    XmlConfigurator.Configure(fInfo);
    LogManager.GetLogger(DefaultLoggerName)?.Info($"Environment={env.EnvironmentName}"); 
    Configuration = configuration;
}

Then, you can create a file like log4net.Production.xml and have it customized according to the environment in use.

like image 41
Tiago B Avatar answered Nov 15 '22 14:11

Tiago B