C# 在 ASP.NET 中包含 log4Net 外部配置文件的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1731513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice to include log4Net external config file in ASP.NET
提问by Martin Buberl
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?
这样做的最佳做法是什么?
采纳答案by Anton
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>
回答by pduncan
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));
}