为什么配置文件中无法识别log4net?
我使用log4net在C#中编写了一个测试控制台应用程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using log4net; using log4net.Config; [assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace Log4Net_Test { class Program { private static readonly ILog log = LogManager.GetLogger(typeof(Program)); static void Main(string[] args) { log.Info("Entering application"); log.Debug("Debug message"); log.Info("Leaving application"); } } }
我的App.config
文件如下所示:
执行测试程序最终会出现以下错误消息:
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML. System.Configuration.ConfigurationErrorsException: Configuration system failed t o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize d configuration section log4net.
我的配置文件有什么问题?
更新1:如configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove the
configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove the
启动section, otherwise the same error appeared. I do not know why the
section, otherwise the same error appeared. I do not know why the
`部分也导致问题。 也许更有经验的人可以讲述和撰写评论。
您还需要在section block中添加log4net
还需要删除启动部分,因为log4net不支持Framework .NET4.5
你可以在这里看到我的类似答案: https : //stackoverflow.com/a/13236410/1783224
或者您可以在文档中看到支持的框架: http : //logging.apache.org/log4net/release/features.html
您还应该记住,部分块< configSections >必须位于< configuration >之下,否则将发生错误:
读取ConfigurationSettings时出现exception。 检查.config文件是否格式正确。
例如: