在ASP.NET中包含log4Net外部配置文件的最佳实践

时间:2021-10-29 03:42:06

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

我已经看到至少两种方法在ASP.NET Web应用程序中包含外部log4net配置文件:

Having the following attribute in your AssemblyInfo.cs file:

在AssemblyInfo.cs文件中具有以下属性:

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

Calling the XmlConfigurator in the Global.asax.cs:

在Global.asax.cs中调用XmlConfigurator:

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

What would be the best practice to do it?

这样做的最佳做法是什么?

2 个解决方案

#1


43  

At startup, call:

在启动时,请致电:

XmlConfigurator.Configure();

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

在Web.config中,在appSettings中指定log4net.Config:

<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:

\ CONFIG \的appSettings \ debug.config:

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

\config\appSettings\staging.config:

\ CONFIG \的appSettings \ staging.config:

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

\config\appSettings\release.config:

\ 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.

更改环境是在Web.config中更新appSettings文件的简单问题。

<appSettings file="config\appSettings\staging.config">
    ...
</appSettings>

#2


4  

I was dissatisfied with the "magic" configuration approach, because I wanted to specify my configuration in a path with an environment variable (%PUBLIC%\MyApp\MySettings.config).

我对“魔术”配置方法不满意,因为我想在具有环境变量的路径中指定我的配置(%PUBLIC%\ MyApp \ MySettings.config)。

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

所以相反,我在app.config中有这个:

<add key="MyLog4NetConfigFile" value="%PUBLIC%\MyApp\MySettings.config"/>

And do this to set my log4net configuration:

并执行此操作以设置我的log4net配置:

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

#1


43  

At startup, call:

在启动时,请致电:

XmlConfigurator.Configure();

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

在Web.config中,在appSettings中指定log4net.Config:

<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:

\ CONFIG \的appSettings \ debug.config:

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

\config\appSettings\staging.config:

\ CONFIG \的appSettings \ staging.config:

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

\config\appSettings\release.config:

\ 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.

更改环境是在Web.config中更新appSettings文件的简单问题。

<appSettings file="config\appSettings\staging.config">
    ...
</appSettings>

#2


4  

I was dissatisfied with the "magic" configuration approach, because I wanted to specify my configuration in a path with an environment variable (%PUBLIC%\MyApp\MySettings.config).

我对“魔术”配置方法不满意,因为我想在具有环境变量的路径中指定我的配置(%PUBLIC%\ MyApp \ MySettings.config)。

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

所以相反,我在app.config中有这个:

<add key="MyLog4NetConfigFile" value="%PUBLIC%\MyApp\MySettings.config"/>

And do this to set my log4net configuration:

并执行此操作以设置我的log4net配置:

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