将Log4Net添加到项目时出错

Mat*_*ynn 16 c# asp.net-mvc log4net

我正在努力将log4net添加到我的MVC5项目中.我做了以下事情;

安装包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>
Run Code Online (Sandbox Code Playgroud)

我在web.config中的configSections中添加了以下内容;

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
Run Code Online (Sandbox Code Playgroud)

我已将以下内容添加到我的Global.asax中

log4net.Config.XmlConfigurator.Configure();
Run Code Online (Sandbox Code Playgroud)

解决方案编译,但是当我尝试运行我的程序时,我得到错误;

HTTP错误500.19 - 内部服务器错误

无法访问请求的页面,因为页面的相关配置数据无效.

无法读取配置节'log4net',因为它缺少节声明

有没有人有任何我做错的事吗?

Rol*_*n C 19

您还需要在web.config中包含它

<configSections>   
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
Run Code Online (Sandbox Code Playgroud)

它应该如下所示,还要确保在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>
Run Code Online (Sandbox Code Playgroud)


kay*_*zam 7

您可能缺少声明配置部分.在web.config中试试这个:

<configuration>
  <configSections>
    <section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  <configSections>
<configuration>
Run Code Online (Sandbox Code Playgroud)


Pra*_*upu 5

注意:您需要在configsections 中的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>
Run Code Online (Sandbox Code Playgroud)