Log4net traceappender 不记录任何内容

发布于 2024-09-17 06:14:52 字数 2144 浏览 5 评论 0原文

我想将 log4net 记录的所有内容重定向到 System.Diagnostics Trace 类。我对应该做什么的理解是将 log4net 指向 system.diagnostics.traceappender,然后配置 system.diagnostics。以下是我的 web.config 中的重要部分:

<log4net>
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
  <immediateFlush value="true" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern"
         value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
  </layout>
</appender>

<root>
  <priority value="DEBUG"/>
  <appender-ref ref="trace"/>
</root>    
</log4net>


<system.diagnostics>
<sources>

  <source name="Console" switchName="DefaultSwitch">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
    </listeners>
  </source>

  <source name="Metabase" switchName="MetabaseSwitch">
    <listeners>
      <add name="MetabaseListener" />
      <remove name="Default" />
    </listeners>
  </source>

  <source name="TextFile" switchName="TextFileSwitch">
    <listeners>
      <add name="TextFileListener" />
      <remove name="Default" />
    </listeners>
  </source>

</sources>
<sharedListeners>
  <!--<add name="ConsoleListener" type="XXX.Manufacturing.Utilities.Diagnostics.ColorConsoleTraceListener,XXX.Manufacturing.Utilities" />-->
  <add name ="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextFile.log" />
  <add name="MetabaseListener" type="XXX.Manufacturing.Utilities.Diagnostics.MetabaseTraceListener,XXX.Metabase.Proxies" />
</sharedListeners>
<switches>
  <add name="MetabaseSwitch" value="Information" />
  <add name="DefaultSwitch" value="Verbose" />
  <add name="TextFileSwitch" value="Verbose"/>
</switches>
</system.diagnostics>

我是否错过了连接事物的关键步骤?如果我绕过 log4net 并仅创建一个新的跟踪源,它将记录到我的源。

I'd like to redirect everything logged by log4net to the System.Diagnostics Trace classes. My understanding of what I should be doing is pointing log4net at system.diagnostics.traceappender, then I configure system.diagnostics. Here's important parts in my web.config:

<log4net>
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
  <immediateFlush value="true" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern"
         value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
  </layout>
</appender>

<root>
  <priority value="DEBUG"/>
  <appender-ref ref="trace"/>
</root>    
</log4net>


<system.diagnostics>
<sources>

  <source name="Console" switchName="DefaultSwitch">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
    </listeners>
  </source>

  <source name="Metabase" switchName="MetabaseSwitch">
    <listeners>
      <add name="MetabaseListener" />
      <remove name="Default" />
    </listeners>
  </source>

  <source name="TextFile" switchName="TextFileSwitch">
    <listeners>
      <add name="TextFileListener" />
      <remove name="Default" />
    </listeners>
  </source>

</sources>
<sharedListeners>
  <!--<add name="ConsoleListener" type="XXX.Manufacturing.Utilities.Diagnostics.ColorConsoleTraceListener,XXX.Manufacturing.Utilities" />-->
  <add name ="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextFile.log" />
  <add name="MetabaseListener" type="XXX.Manufacturing.Utilities.Diagnostics.MetabaseTraceListener,XXX.Metabase.Proxies" />
</sharedListeners>
<switches>
  <add name="MetabaseSwitch" value="Information" />
  <add name="DefaultSwitch" value="Verbose" />
  <add name="TextFileSwitch" value="Verbose"/>
</switches>
</system.diagnostics>

Did I miss a crucial step linking things up? If I bypass log4net and just create a new tracesource it will log to my sources.

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

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

发布评论

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

评论(2

寻梦旅人 2024-09-24 06:14:52

我添加了对 XmlConfigurator.Configure() 的调用并打开了内部日志记录。我看到 log4net 正在记录日志,但没有任何内容到达跟踪系统。在使用我的 app.config 一段时间后,我发现一个有效的配置,最显着的变化似乎是放弃了 Systems.Diagnostics 配置中的源并确保设置了 log4net level 属性。工作配置部分:

<log4net>
  <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
    <immediateFlush value="true" />
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
    </layout>
  </appender>

  <root>
    <level value="ALL"/>
    <appender-ref ref="trace"/>
  </root>    
</log4net>

<system.diagnostics>
  <trace autoflush="true" >
    <listeners>
      <add name="TextFileListener" />
      <add name="MetabaseListener" />
    </listeners>
  </trace>
  <sharedListeners>
    <add name ="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextFile.log" />
    <add name="MetabaseListener" type="XXXX.Manufacturing.Utilities.Diagnostics.MetabaseTraceListener, XXXX.Metabase.Proxies" />
  </sharedListeners>
</system.diagnostics>

I added the call to XmlConfigurator.Configure() and turned on internal logging. What I saw was log4net was logging, but nothing was reaching the trace system. After playing with my app.config for a while I found a configuration that worked, most notable changes seemed to be ditching the sources in my Systems.Diagnostics config and making sure the log4net level attribute was set. Working config sections:

<log4net>
  <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
    <immediateFlush value="true" />
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
    </layout>
  </appender>

  <root>
    <level value="ALL"/>
    <appender-ref ref="trace"/>
  </root>    
</log4net>

<system.diagnostics>
  <trace autoflush="true" >
    <listeners>
      <add name="TextFileListener" />
      <add name="MetabaseListener" />
    </listeners>
  </trace>
  <sharedListeners>
    <add name ="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextFile.log" />
    <add name="MetabaseListener" type="XXXX.Manufacturing.Utilities.Diagnostics.MetabaseTraceListener, XXXX.Metabase.Proxies" />
  </sharedListeners>
</system.diagnostics>
向日葵 2024-09-24 06:14:52

您是否调用了 XmlConfigurator.Configure(); 方法?

如果这不是问题,那么您可以打开内部调试(解释这里)或者配置一个控制台附加器并看看它是否有效。

编辑:我对跟踪系统不太熟悉,但是如果您按如下方式配置跟踪侦听器,您应该获得 log4net 输出:

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add
         name="textWriterTraceListener"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="C:\temp\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>

Did you call the XmlConfigurator.Configure(); method?

If that is not the problem then you can either turn on internal debugging (explained here) or maybe configure a console appender and see if that is working.

Edit: I am not that familiar with the trace system, but if you configure a trace listener as follows you should get the log4net output:

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add
         name="textWriterTraceListener"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="C:\temp\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文