log4net仅适用于多个项目解决中的一个项目

时间:2017-07-18 17:20:16

标签: c# log4net

我有一个多项目解决方案,它使用Log4Net进行日志记录。 Log4Net设置为记录到文件和数据库。文件日志记录部分按预期工作。数据库部分仅适用于一个项目。

namespace Solution.ProjectOne
{
     class A
     {
         private ILog log = LogManager.GetLogger(typeof(A));

         public A()
         {
             log4net.config.XmlConfigurator();
         }

         public void AMethod()
         {
             log.Info("I work"); // Works
         }

         public B LoadB()
         {
             // B is loaded via reflection in our solution
             return new B();
         }
     }

}

namespace solution.ProjectTwo
{   
    class B
    {
        private ILog log = LogManager.GetLogger(typeof(B));

        public B()
        {}

        public void BMethod()
        {
            // This logs to the file but not the database.
            log.Error("I do not work"); 
        }
    }
}   



<log4net>
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">

      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="server=<SERVER>;database=<DATABASE>; UID=<USERNAME>;Password=<PASSWORD>;Enlist=false; Connect Timeout=60; Max Pool Size=100" />
      <commandText value="dbo.SP_SaveLog" />
      <commandType value="StoredProcedure" /> 
      <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="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout" value="%thread" />
      </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="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="WARN"/>
      </filter>
    </appender>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    </appender>
    <root>
      <level value="debug" />
      <appender-ref ref="ADONetAppender" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>

0 个答案:

没有答案