写入两个位置不起作用

发布于 2024-10-25 03:31:49 字数 3102 浏览 1 评论 0原文

我觉得这应该是可能的,但我只能让 log4net 一次访问一个附加程序。

<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Logs\webclient\Admin.Web\logs\admin.log" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <!--Database appender-->
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="10" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <connectionString value="Data Source=(localhost); Database=Error_Logs; Persist Security Info=True;User ID=;Password=" />
    <commandText value="INSERT INTO [GeneralServiceLog] ([dtLog],[vchLevel],[vchLogger],[vchMessage],[vchException])
     VALUES( @log_date, @log_level, @logger, @message, @ex)" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
    </parameter>
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="50" />
      <layout type="log4net.Layout.PatternLayout" value="%level" />
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout" value="%logger" />
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.PatternLayout" value="%message" />
    </parameter>
    <parameter>
      <parameterName value="@ex" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>
  <root>
    <level value="ERROR" />
    <appender-ref ref="AdoNetAppender" />
  </root>
  <Logger>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
  </Logger>    
</log4net>

我希望将错误级别消息发送到数据库,将调试级别消息发送到文本文件,但所有消息都将写入文件。

我缺少什么?

谢谢!

I feel this should be possible but I can only get log4net to go to one appender at a time.

<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Logs\webclient\Admin.Web\logs\admin.log" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <!--Database appender-->
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="10" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <connectionString value="Data Source=(localhost); Database=Error_Logs; Persist Security Info=True;User ID=;Password=" />
    <commandText value="INSERT INTO [GeneralServiceLog] ([dtLog],[vchLevel],[vchLogger],[vchMessage],[vchException])
     VALUES( @log_date, @log_level, @logger, @message, @ex)" />
    <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
    </parameter>
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="50" />
      <layout type="log4net.Layout.PatternLayout" value="%level" />
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout" value="%logger" />
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.PatternLayout" value="%message" />
    </parameter>
    <parameter>
      <parameterName value="@ex" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>
  <root>
    <level value="ERROR" />
    <appender-ref ref="AdoNetAppender" />
  </root>
  <Logger>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
  </Logger>    
</log4net>

I want the ERROR level messages to go to the database and DEBUG level messages to go to the text file but all the messaging are just going to file.

What am I missing?

Thanks!

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

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

发布评论

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

评论(1

慢慢从新开始 2024-11-01 03:31:49

您需要按如下方式配置根记录器:

<root>
   <level value="DEBUG" />
   <appender-ref ref="AdoNetAppender" />  
   <appender-ref ref="RollingLogFileAppender" />
</root>  

然后向数据库附加程序添加过滤器:

<filter type="log4net.Filter.LevelRangeFilter">
   <levelMin value="ERROR" /> 
   <levelMax value="FATAL" />      
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

更新:
您的配置问题在于您将数据库附加程序分配给未命名的记录器,因此您的附加程序很可能永远不会被调用。在 log4net 中,您可以将附加程序分配给 记录器,但还没有达到水平。

You need to configure the root logger as follows:

<root>
   <level value="DEBUG" />
   <appender-ref ref="AdoNetAppender" />  
   <appender-ref ref="RollingLogFileAppender" />
</root>  

then you add a filter to the database appender:

<filter type="log4net.Filter.LevelRangeFilter">
   <levelMin value="ERROR" /> 
   <levelMax value="FATAL" />      
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

Update:
The problem with your configuration is that you assign the db appender to an unnamed logger and thus your appender in all likelihood never gets called. In log4net you can assign appenders to loggers, but not to levels.

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