Web.Config 中的 Log4Net 部分生成错误

发布于 2024-10-31 16:55:07 字数 1357 浏览 1 评论 0原文

所以我尝试在我的 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>

并且我仍然会得到相同的错误。

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

So I am trying to set up Log4Net in my Web .NET 4.0 application. I have added the correct .dll to my project and have appended the following to my Web.Config file as starters:

<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>

However, if I append the "log4net" section to Web.Config, I will receive the error message

Unable to start debugging on the web server. See help for common
configuration problems.....

Make sure the server is running correctly. Verify there are no syntax
errors in the web.config........

NOTE
I can remove all the internals of this section and leave only the declaration:

<log4net></log4net>

and I will still get the same error.

Can someone give me some pointers on how to track down this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-11-07 16:55:07

对于不确定如何开始的开发人员,以下可能会有所帮助

app.config 中的 ConfigSections

请记住告诉您的应用程序,如果您打算使用某个库正在引入自定义配置部分,我我不太确定它是否是强制性的,但我总是将其用作根 标记中的第一部分。

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

app.config 中的 log4net 配置

log4net 中提供了多种不同的附加程序,但我通常使用 RollingFileAppender,因此我在本示例中使用相同的附加程序,您可以找到其余的 此处

<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>

更新 AssemblyInfo.cs 文件

每当我必须创建新项目时,我总是会错过这一步。因此,请记住,您必须告诉您的应用程序监视 XMLConfigurator 来执行 log4net 的配置,因此 AssemblyInfo.cs 文件末尾包含以下行:

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

开始使用

请记住包含log4net.dll 的引用然后使用以下代码行在类中初始化记录器

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

最后让我们使用它就像下面的

log.Info("Hello log4net");

快乐伐木:)

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 :)

情深已缘浅 2024-11-07 16:55:07

log4net 文档 主页查看示例配置。
您很可能丢失了所需的标签。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文