.net core 2.1 log4net with multip environments
Asked Answered
P

2

11

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?

Privily answered 28/1, 2019 at 20:49 Comment(0)
A
10

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.

Aguayo answered 31/1, 2019 at 11:31 Comment(1)
While I feel that @TiagoBrenck had a great answer (that works very well), I feel this should be the "Accepted Answer" as it makes use of the built-in Log4NetCore.PropertyOverrides.Putrid
N
5

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.

Neutrality answered 28/1, 2019 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.