log4net'缺少一个section声明
本文关键字:一个 section 声明 log4net | 更新日期: 2023-09-27 18:16:34
我正在努力将log4net添加到我的MVC5项目中。我已经做了以下工作:
安装包log4net
已成功安装(我假设)log4net
我在我的网页上添加了以下内容。
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs'ApiLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
和我有以下添加到我的configSections在web.config;
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
我在我的Global.asax.cs;
log4net.Config.XmlConfigurator.Configure();
解决方案编译,但是当我试图运行我的程序时,我得到错误;
HTTP错误500.19 -内部服务器错误
请求的页面不能被访问,因为相关的页面的配置数据无效。
无法读取配置节'log4net',因为它是缺少一个section声明
有人知道我做错了什么吗?
您还需要在web.config
中包含此内容。<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
应该如下所示,同时确保你的应用程序bin文件夹中有log4net.dll, log4net.xml
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
</appSettings>
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd.lo'g" />
<maximumFileSize value="5MB" />
<maxSizeRollBackups value="-1" />
<countDirection value="1" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level [%thread] [%aspnet-session{SessionId}] %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
您可能没有声明配置部分。在web.config中试试:
<configuration>
<configSections>
<section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<configSections>
<configuration>
注意:您需要在configsections的sectionGroup之外有section name
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
</sectionGroup>