在 ASP.NET 中包含 log4Net 外部配置文件的最佳实践
我至少看到了两种在 ASP.NET Web 应用程序中包含外部 log4net 配置文件的方法:
在 AssemblyInfo.cs 文件中具有以下属性:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]
在 Global.asax.cs 中调用 XmlConfigurator:
protected void Application_Start()
{
XmlConfigurator.Configure(new FileInfo("Log.config"));
}
最佳实践是什么去做吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
启动时,调用:
在您的 Web.config 中,在 appSettings 中指定 log4net.Config:
此特殊设置允许您更改日志配置,而无需重新编译。对于在多个环境之间移动特别有帮助。
示例
考虑以下项目文件结构:
每个环境的应用程序和日志记录配置都是不同的。对日志记录配置的引用保留在应用程序设置中。
\config\appSettings\debug.config:
\config\appSettings\staging.config:
\config\appSettings\release.config:
更改环境只需更新 Web.config 中的 appSettings 文件即可。
At startup, call:
In your Web.config, specify log4net.Config in appSettings:
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:
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\staging.config:
\config\appSettings\release.config:
Changing environments is a simple matter of updating the appSettings file in Web.config.
我对“神奇”的配置方法不满意,因为我想在带有环境变量(%PUBLIC%\MyApp\MySettings.config)的路径中指定我的配置。
因此,我在 app.config 中添加了以下内容:
并执行此操作来设置我的 log4net 配置:
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).
So instead, I have this in my app.config:
And do this to set my log4net configuration: