Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 996
Martin Buberl Avatar asked Nov 13 '09 19:11

Martin Buberl


People also ask

Where is log4net configuration file?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.


1 Answers

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:

\config\log4net\debug.config \config\log4net\staging.config \config\log4net\release.config \config\appSettings\debug.config \config\appSettings\staging.config \config\appSettings\release.config 

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

\config\appSettings\debug.config:

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

\config\appSettings\staging.config:

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

\config\appSettings\release.config:

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

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

<appSettings file="config\appSettings\staging.config">     ... </appSettings> 
like image 198
Anton Avatar answered Oct 25 '22 04:10

Anton