log4net 不同的 appenders 插入不同的表

log4net different appenders insert different tables

我的配置文件中有 2 个 appender,它们被配置为插入不同的表,"Log" 和 "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>

这是记录器class;

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);
            }
        }

在控制器中没有看到错误,所以问题一定出在配置中...我搜索并尝试添加

additivity="false"

属性 到 "AdoNetAppenderForFirms" 元素,但没有任何改变。那么你有什么建议来解决这个问题吗?

您可以通过将记录器添加到您的配置中来完成此操作,而不是在根级别添加附加程序:

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