为什么 log4net 不记录 nhibernate 信息

发布于 2024-10-21 08:02:28 字数 8272 浏览 2 评论 0原文

我的 Visual Studio 解决方案包含:

  1. [DLL] Sol.DataAccess(NHibernate sessionManager)
  2. [DLL] Sol.Core(模型和存储库)
  3. [MVC] Sol.WebMvc(控制器,视图)

我的应用程序包含的所有内容是(nhibernate.dll [v3.1]) 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());
        }
     }

在我的日志文件 - 日志/日志中。 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 信息(信息、调试......)

这些 dll 的版本不兼容吗?

My Visual Studio Solution contains:

  1. [DLL] Sol.DataAccess (NHibernate sessionManager)
  2. [DLL] Sol.Core (Models and Repository)
  3. [MVC] Sol.WebMvc (Controler, View)

All my application contains are (nhibernate.dll [v3.0] and log4net.dll[v1.2.10])

I have 3 configs:

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>

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

in my test controller i have:

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

In my log file - 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.

QUESTION:
Why log4net doesn't log NHibernate information (info, debug ....)

Aren't the versions of these dlls compatible?

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

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

发布评论

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

评论(2

有深☉意 2024-10-28 08:02:28

我创建了一个空的 ASP.Net MVC3 项目。并花费大量时间试图解决这个问题。
我发现VS2010有bug。 当您在项目中引用 dll 时,Visual Studio 2010 不会将 dll 复制到 bin 中。

我将 log4net.dll 手册放在 bin 文件夹中并且工作正常。 (有趣的是,Logger.Error("fake error") 在 bin 文件夹中没有 log4net.dll 的情况下也可以正常工作...)

在此处输入图像描述

I have create an empty ASP.Net MVC3 project. and loss much time trying to fix this issue.
And I find VS2010 bug. Visual Studio 2010 don't copy dll's in bin when you refer it in project.

I put log4net.dll manual in my bin folder and work fine. (Interesting thing is that Logger.Error("fake error") work fine without log4net.dll in bin folder ...)

enter image description here

滥情稳全场 2024-10-28 08:02:28

首先:您提到“log4net.config”,但您没有在任何地方包含它。在 web.config 中配置它的方式,您实际上应该在 web.config 中包含一个名为 的部分,而不是像一个单独的文件。

如果您不想在 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上,您将查找完整示例

First off: you speak of "log4net.config", but you don't include that anywhere. The way you configure it in web.config, you should actually include a section called <log4net> inside your web.config, not as a separate file.

If you don't want it in your web.config, you can remove the log4net related sections altogether and add the following line to yur global.asax.cs:

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

Also, possibly you miss the priority setting, not entirely sure this helps, but give it a try:

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

Also, to finetune (because you'll get a lot messages), use something like this:

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

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

On nhibernate.info you'll find a full example.

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