Mar*_*erl 25 c# asp.net log4net
我已经看到至少两种方法在ASP.NET Web应用程序中包含外部log4net配置文件:
在AssemblyInfo.cs文件中具有以下属性:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]
Run Code Online (Sandbox Code Playgroud)
在Global.asax.cs中调用XmlConfigurator:
protected void Application_Start()
{
XmlConfigurator.Configure(new FileInfo("Log.config"));
}
Run Code Online (Sandbox Code Playgroud)
这样做的最佳做法是什么?
Ant*_*ton 45
在启动时,请致电:
XmlConfigurator.Configure();
Run Code Online (Sandbox Code Playgroud)
在Web.config中,在appSettings中指定log4net.Config:
<add key="log4net.Config" value="Log.config" />
Run Code Online (Sandbox Code Playgroud)
此特殊设置允许您更改日志配置,而无需重新编译.特别适合在多个环境之间移动.
例
考虑以下项目文件结构:
\config\log4net\debug.config
\config\log4net\staging.config
\config\log4net\release.config
\config\appSettings\debug.config
\config\appSettings\staging.config
\config\appSettings\release.config
Run Code Online (Sandbox Code Playgroud)
针对每个环境区分应用程序和日志记录配置.在应用程序设置中维护对日志记录配置的引用.
\ config\appSettings\debug.config:
<appSettings>
<add key="log4net.Config" value="config\log4net\debug.config" />
...
</appSettings>
Run Code Online (Sandbox Code Playgroud)
\ config\appSettings\staging.config:
<appSettings>
<add key="log4net.Config" value="config\log4net\staging.config" />
...
</appSettings>
Run Code Online (Sandbox Code Playgroud)
\ config\appSettings\release.config:
<appSettings>
<add key="log4net.Config" value="config\log4net\release.config" />
...
</appSettings>
Run Code Online (Sandbox Code Playgroud)
更改环境是在Web.config中更新appSettings文件的简单问题.
<appSettings file="config\appSettings\staging.config">
...
</appSettings>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31622 次 |
最近记录: |