为什么log4net不记录nhibernate信息

时间:2011-03-11 16:52:37

标签: asp.net-mvc nhibernate logging web-config log4net

我的Visual Studio解决方案包含:

  1. [DLL] Sol.DataAccess(NHibernate sessionManager)
  2. [DLL] Sol.Core(模型和存储库)
  3. [MVC] Sol.WebMvc(Controler,View)
  4. 我的所有应用程序都包含(nhibernate.dll [v3.0]和log4net.dll [v1.2.10])

    我有3个配置:

    的web.config:

    <configuration>
        <configSections>
             <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false" />
             <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
        </configSections>
    </configuration>
    

    nhibernate.config:

    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
      <session-factory name="...">
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string_name">...</property>
        <property name="adonet.batch_size">10</property>
        <property name="show_sql">true</property>
        <property name="generate_statistics">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="use_outer_join">true</property>
        <property name="max_fetch_depth">2</property>
        <property name="command_timeout">60</property>
        <property name="adonet.batch_size">25</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
        <property name="current_session_context_class">web</property>
        <property name="cache.use_query_cache">true</property>
        <property name="cache.provider_class">NHibernate.Caches.SysCache2.SysCacheProvider, NHibernate.Caches.SysCache2</property>
        <mapping assembly="..."/>
      </session-factory>
    </hibernate-configuration>
    

    和log4net.config:

    <log4net>
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <!--for release-->
        <!--<bufferSize value="10" />-->
        <!--for debug-->
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="Data Source=xxxxx; Initial Catalog=xxxx; User Id=xxxx; Password=xxxxx; App=xxxx" />
        <commandText value="INSERT INTO Logs ([Application],[Host],[User],[Date],[Thread],[Level],[Operation],[Logger],[Message],[Exception]) VALUES (@app, @hostName, @userName, @log_date, @thread, @log_level, @operation, @logger, @message, @exception)" />
        <parameter>
          <parameterName value="@app" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="xxxx" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@hostName" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%property{hostName}" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@userName" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%property{userName}" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@log_date" />
          <dbType value="DateTime" />
          <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        <parameter>
          <parameterName value="@thread" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@log_level" />
          <dbType value="String" />
          <size value="50" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@operation" />
          <dbType value="String" />
          <size value="50" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%property{Operation}" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@logger" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@message" />
          <dbType value="String" />
          <size value="4000" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@exception" />
          <dbType value="String" />
          <size value="2000" />
          <layout type="log4net.Layout.ExceptionLayout" />
        </parameter>
      </appender>
    
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="Logs/Logs.txt"/>
        <appendToFile value="true"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
        </layout>
      </appender> 
    
      <appender name="Console" type="log4net.Appender.AspNetTraceAppender">
        <!--A1 uses PatternLayout-->
        <layout type="log4net.Layout.PatternLayout">
          <!--Print the date in ISO 8601 format-->
          <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline"/>
        </layout>
      </appender>
    
      <root>
        <level value="ALL"/>
        <appender-ref ref="Console"/>
        <appender-ref ref="FileAppender"/>
        <appender-ref ref="AdoNetAppender"/>
      </root>
    </log4net>
    

    Global.cs:

    protected void Application_Start()
        {
           ...
            // Configuration
    
            #region log4net
            // log4net.config
            System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("~/log4net.config"));
            if (fi != null && fi.Exists)
            {
                // Code that runs on application startup
                log4net.Config.XmlConfigurator.Configure(fi);
            }
    
            // web.config
            //log4net.Config.XmlConfigurator.Configure();
    
            // set properti hostName
            log4net.GlobalContext.Properties["hostName"] = Dns.GetHostName();
            #endregion
    
            #region NHibernate
            //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            var factory = NHibernateSessionManager.ConfigureFromFile(Server.MapPath("~/hibernate.config"));
            #endregion
        }
    

    在我的测试控制器中我有:

    public class TestController : BaseController
        {
            [NHibernateSession]
            public ActionResult Index()
            {
                 Logger.Error("fake error", new Exception());
            }
         }
    

    在我的日志文件中 - Logs / Logs.txt:

    2011-03-11 18:19:23,097 [8] ERROR System.Web.Mvc.Controller [(null)] - fake error
    System.Exception: Exception of type 'System.Exception' was thrown.
    

    问题: 为什么log4net不记录NHibernate信息(info,debug ....)

    这些dll的版本不兼容吗?

2 个答案:

答案 0 :(得分:3)

首先关闭:你说的是“log4net.config”,但你没有把它包括在内。在web.config中配置它的方式,实际上应该在web.config中包含一个名为<log4net> 的部分,而不是单独的文件。

如果您不想在web.config中使用它,则可以完全删除与log4net相关的部分,并将以下行添加到您的global.asax.cs中:

log4net.Config.XmlConfigurator.ConfigureAndWatch(
      New FileInfo(Server.MapPath("~/yourreleativepath/log4net.config")))

此外,您可能错过了优先级设置,但并不完全确定这会有所帮助,但请尝试一下:

<root>
  <priority value="DEBUG"/>
  <appender-ref ref="Console"/>
  <appender-ref ref="FileAppender"/>
  <appender-ref ref="AdoNetAppender"/>
</root>

另外,为了微调(因为你会得到很多消息),请使用以下内容:

<logger name="NHibernate.SQL">
    <level value="DEBUG"/>
</logger>

<logger name="NHibernate">
    <level value="WARN"/>
</logger>

在nhibernate.info上你会find a full example

答案 1 :(得分:3)

我创建了一个空的ASP.Net MVC3项目。并且花了很多时间来解决这个问题。 我发现VS2010的bug。 当您在项目中引用dll时,Visual Studio 2010不会在bin中复制dll。

我将log4net.dll手册放在我的bin文件夹中并正常工作。 (有趣的是Logger.Error(“假错误”)在bin文件夹中没有log4net.dll的情况下工作正常......)

enter image description here