9

所以我试图在我的 Web .NET 4.0 应用程序中设置 Log4Net。我已将正确的 .dll 添加到我的项目中,并将以下内容作为启动器附加到我的 Web.Config 文件中:

<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
</root>
</configSections>
 <log4net debug="true">
 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\TestProj\\TestLog.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>

但是,如果我将“log4net”部分附加到 Web.Config,我将收到错误消息

无法在 Web 服务器上开始调试。有关常见配置问题,请参阅帮助......

确保服务器正常运行。验证 web.config 中没有语法错误............

注意 我可以删除本节的所有内部内容,只保留声明:

<log4net></log4net>

我仍然会得到同样的错误。

有人可以给我一些关于如何追踪此错误的指示吗?

4

2 回答 2

16

For developers who are not sure exactly how to get started following might be a help

ConfigSections in app.config

Remember to tell your application that a library is introducing a custom configuration section are you are intended to utilize, I am not perfectly sure if it is mandatory or not but I always use it as first section within root <configuration> tag.

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

log4net config in app.config

There are quite a variety of different appenders available in log4net but I usually use RollingFileAppender so I am using the same one in this sample, you can find rest of those here.

<log4net>
    <!-- file appender -->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:/logs/my_log_file.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <maxSizeRollBackups value="30"/>
      <datePattern value=".yyyy-MM-dd"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
  </log4net>

Update AssemblyInfo.cs file

I always miss this step whenever I have to create a new project. So remember you have to tell your application to watch for XMLConfigurator to perform configuration of log4net, so following line goes at the end of AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Get Started

Remember to include the reference of log4net.dll then use following line of code to initialize logger within a class

private static ILog log = LogManager.GetLogger(typeof(MyClass));

And at the end lets use it like following

log.Info("Hello log4net");

Happy Logging :)

于 2013-10-09T23:00:47.990 回答
1

Take a look at a sample configurations at log4net documentation homepage.
Chances are you've misplaced required tags.

于 2011-04-10T13:34:57.117 回答