Best practice to include log4Net external config file in ASP.NET

I have seen at least two ways to include an external log4net config file in an ASP.NET web application:

Having the following attribute in your AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]

Calling the XmlConfigurator in the Global.asax.cs:

protected void Application_Start()
{
    XmlConfigurator.Configure(new FileInfo("Log.config"));
}

What would be the best practice to do it?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

At startup, call:

XmlConfigurator.Configure();

In your Web.config, specify log4net.Config in appSettings:

<add key="log4net.Config" value="Log.config" />

This special setting allows you to change the log configuration without having to recompile. Especially helpful for moving between multiple environments.

Example

Consider the following project file structure:

configlog4netdebug.config
configlog4netstaging.config
configlog4netrelease.config
configappSettingsdebug.config
configappSettingsstaging.config
configappSettingsrelease.config

Application and logging configurations are distinguished for each environment. References to the logging configurations are maintained in the application settings.

configappSettingsdebug.config:

<appSettings>
    <add key="log4net.Config" value="configlog4netdebug.config" />
    ...
</appSettings>

configappSettingsstaging.config:

<appSettings>
    <add key="log4net.Config" value="configlog4netstaging.config" />
    ...
</appSettings>

configappSettingsrelease.config:

<appSettings>
    <add key="log4net.Config" value="configlog4netrelease.config" />
    ...
</appSettings>

Changing environments is a simple matter of updating the appSettings file in Web.config.

<appSettings file="configappSettingsstaging.config">
    ...
</appSettings>

Method 2

I was dissatisfied with the “magic” configuration approach, because I wanted to specify my configuration in a path with an environment variable (%PUBLIC%MyAppMySettings.config).

So instead, I have this in my app.config:

<add key="MyLog4NetConfigFile" value="%PUBLIC%MyAppMySettings.config"/>

And do this to set my log4net configuration:

var configFile = ConfigurationManager.AppSettings.Get("MyLog4NetConfigFile");
if( !string.IsNullOrEmpty(configFile))
{
    configFile = Environment.ExpandEnvironmentVariables(configFile);
    XmlConfigurator.Configure(new FileInfo(configFile));
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x