log4net不同的appender插入不同的表

时间:2016-09-28 08:59:24

标签: log4net

我的配置文件中有2个appender,配置为插入到不同的表中," Log"和#34; LogFirms",但不知何故两个记录器都插入到两个表中。

<log4net debug="true">
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />  
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[ClientIp],[Username],[Controller],[Action],[Parameters]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @clientip, @username, @controller, @action, @parameters)" />
      ...
    </appender>
    <appender name="AdoNetAppenderForFirms" type="log4net.Appender.AdoNetAppender" additivity="false">
      <bufferSize value="1" />
      <commandText value="INSERT INTO LogFirms ([Date],[Thread],[Level],[Logger],[Message],[Exception],[ClientIp],[Username],[Controller],[Action],[Parameters]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @clientip, @username, @controller, @action, @parameters)" />
      ...
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
      <appender-ref ref="AdoNetAppenderForFirms" />
    </root>
  </log4net>

这是记录器类;

public static class Logger
    {
        private static ILog log { get; set; }
        private static ILog logFirms { get; set; }
        static Logger()
        {
            log = LogManager.GetLogger("AdoNetAppender");
            logFirms = LogManager.GetLogger("AdoNetAppenderForFirms");
        }
public static void Error(string controller, string action, string parameters, object msg, Exception ex)
        {
            if (controller == "AccountController" && logFirms.IsErrorEnabled)
            {
                logFirms.Error(msg, ex);
            }
            else if (log.IsErrorEnabled)
            {
                log.Error(msg, ex);
            }
        }

控制器中没有出现错误,因此问题必须在配置中...我搜索并尝试添加

  

加=&#34;假&#34;

属于&#34; AdoNetAppenderForFirms&#34;元素,但没有改变。所以你有什么建议来解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您可以通过向配置添加记录器而不是根级别的追加程序来执行此操作:

<root>
  <level value="ALL" />
</root>

<logger name="AdoNetAppender" additivity="false">
    <level value="ALL" />
    <appender-ref ref="AdoNetAppender" />
</logger>
<logger name="AdoNetAppenderForFirms" additivity="false">
    <level value="ALL" />
    <appender-ref ref="AdoNetAppenderForFirms" />
</logger>