log4net单独的配置文件

log4net separate configuration file

我在我的项目中使用 log4net。 到目前为止,我使用 App.config 文件作为配置文件, 但现在我想把它放在不同的文件中。

这是我所做的:

我将 <log4net> ... </log4net> 移动到新文件

(<log4net> 就在 <?xml version="1.0" encoding="utf-8" ?> 之后)

App.config 上,我更改了如下代码:

<configSections>
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net configSource="log4net.config"/>

并更改了这一行(我怀疑这是有问题的行):

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

虽然这行没有写入 Assemblyinfo.cs。不知道我是否应该...

值得一提的是,只要 log4net.config 文件位于 bin\debug 文件夹内,这就可以正常工作,所以这就是为什么我怀疑程序集的问题所在,我想该文件在 App.config 文件旁边而不是在 bin 文件夹中,在解决方案中很容易看到。另外,如果我在其中放置一个包含 log4net.config 完整路径的字符串,它也可以工作,但是这会在另一台计算机上使用它时出现问题,我怎样才能让它工作?谢谢!

确保 log4net.config 文件的 属性 "Copy to output directory" 设置为 "Copy if newer"。这样,当您 运行 您的项目时,它总是被复制到 bin/debug 或 bin/release 文件夹。