我正在努力将log4net添加到我的MVC5项目中。我做了以下事情;
Install-Package log4net
已成功安装(我假设)log4net
我已将以下内容添加到cofiguration部分中的web.config中;
<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>
我在web.config中的configSections中添加了以下内容;
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
我已将以下内容添加到我的Global.asax.cs;
log4net.Config.XmlConfigurator.Configure();
解决方案编译,但是当我尝试运行我的程序时,我收到了错误;
HTTP错误500.19 - 内部服务器错误
由于相关,无法访问请求的页面 页面的配置数据无效。
配置部分&#39; log4net&#39;因为它是无法阅读的 缺少部分声明
有没有人知道我做错了什么?
答案 0 :(得分:19)
您还需要在web.config中包含此内容
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
它应如下所示,还要确保在Application 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>
答案 1 :(得分:7)
您可能缺少声明配置部分。在web.config中试试这个:
<configuration>
<configSections>
<section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<configSections>
<configuration>
答案 2 :(得分:4)
注意:您需要在配置
中将部分名称置于sectionGroup 之外<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>