在NHibernate中使用企业库日志应用程序块

在NHibernate中使用企业库日志应用程序块,nhibernate,log4net,enterprise-library,Nhibernate,Log4net,Enterprise Library,我们正试图将NHibernate集成为我们的OR/M,然而,我们目前正在使用企业库的日志应用程序块。我知道NHibernate使用log4net来记录。有人举过如何使用企业库记录NHibernate相关日志的例子吗?为什么不让NHibernate使用log4net呢?是的,您必须管理两个,否则您必须为log4net编写一个适配器以登录到EntLibrary 我也使用EntLibrary,只需处理nHibernate的Log4Net。在他们的开发讨论组中,他们讨论了将log4net作为一个依赖项删

我们正试图将NHibernate集成为我们的OR/M,然而,我们目前正在使用企业库的日志应用程序块。我知道NHibernate使用log4net来记录。有人举过如何使用企业库记录NHibernate相关日志的例子吗?

为什么不让NHibernate使用log4net呢?是的,您必须管理两个,否则您必须为log4net编写一个适配器以登录到EntLibrary


我也使用EntLibrary,只需处理nHibernate的Log4Net。在他们的开发讨论组中,他们讨论了将log4net作为一个依赖项删除,但我认为还没有做任何工作。

编写自己的log4net附加器,将其写入EL记录器。这是一种适配器模式

log4net.appender.AppenderSkeleton

重写骨架类中的
Append
事件处理程序,并在其中 显示
RenderedMessage
,如下所示:

using System;
using log4net;
using System.Windows.Forms;

namespace MyAppender
{
    public class CustomAppender : log4net.Appender.AppenderSkeleton
    {
        protected override void Append(log4net.spi.LoggingEvent log)
        {
            // log to EL logger based on log properties.
        }
    }
}
然后需要配置log4net配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <log4net>
        <appender name="MyAppender" type="MyAppender.CustomAppender,CustomAppender">
            <threshold value="DEBUG"/>
        </appender>

        <root>
            <level value="DEBUG" />
                <appender-ref ref="MyAppender" />
        </root>
    </log4net>
</configuration>


我还没有测试过这个,但它应该可以让你继续。

这是我一直在思考的问题。我可以为您确认,因此,您必须按照Josh的说明编写一个附录


编辑:从NHibernate 3开始,不再存在硬依赖关系。

NHibernate 3及以上版本允许您通过获取必要的二进制文件并进行配置,将其通用日志记录实现与Microsoft的Enterprise Library 5.0一起使用。看看吧

我还没有在网上找到这个问题,所以我想我会把它贴出来,即使这个问题已经很老了。这是一个配置文件(您必须添加必要的引用):


我同意这一点。还有一个问题是NHibernate记录的内容量。如果同时启用这两个日志记录选项,您可以在几分钟内获得兆字节的数据(并真正降低性能)。这是你只想在短时间内完成的事情。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
 <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
 <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>

  <loggingConfiguration name="" tracingEnabled="true"
    defaultCategory="General">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="Enterprise Library Logging" formatter="Text Formatter"
        log="trace.log" machineName="." traceOutputOptions="DateTime" />
      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="MyLogNameGoesHere.txt" formatter="Text Formatter" traceOutputOptions="DateTime" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Event Log Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Event Log Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

  <appSettings>
    <add key="nhibernate-logger" value="NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, NHibernate.Logging.CommonLogging" />
  </appSettings>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.EntLib.EntLibLoggerFactoryAdapter, Common.Logging.EntLib50"/>
    </logging>
  </common>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.2.1.4000" newVersion="3.2.1.4000" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>