【问题标题】:Using Enterprise Library logging application block in NHibernate在 NHibernate 中使用 Enterprise Library 日志记录应用程序块
【发布时间】:2009-03-20 18:44:35
【问题描述】:

我们正在尝试将 NHibernate 集成为我们的 OR/M,但是,我们目前正在使用 Enterprise Library 的日志记录应用程序块。我知道 NHibernate 使用 log4net 来记录。有没有人有关于如何使用 Enterprise Library 记录 NHibernate 相关日志的示例?

【问题讨论】:

    标签: nhibernate log4net enterprise-library


    【解决方案1】:

    编写您自己的 log4net 附加程序,用于写入 EL 记录器。这是一个适配器模式。

    log4net.Appender.AppenderSkeleton 继承一个新的/自定义的 appender 类

    覆盖骨架类中的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>
    

    我没有对此进行测试,但它应该可以帮助你。

    【讨论】:

      【解决方案2】:

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

      我也使用 EntLibrary,只是处理用于 nHibernate 的 Log4Net。在他们的开发讨论组中,他们谈到了删除 log4net 作为一种依赖,但我认为尚未对此进行任何工作。

      【讨论】:

      • 我同意这一点。还有一个额外的问题是 NHibernate 记录的内容量。如果您同时打开这两个日志记录选项,您可以在几分钟内获得数兆字节的数据(并且真的会降低性能)。这只是你想在短时间内使用的东西。
      【解决方案3】:

      这是我一直在想的关于自己的事情。我可以为您确认NHibernate has a hard dependency on Log4Net,因此,您必须按照 Josh 的说明编写一个附加程序。

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

      【讨论】:

        【解决方案4】:

        NHibernate 3 及更高版本允许您通过获取必要的二进制文件并配置它们,将其通用日志实现与 Microsoft 的 Enterprise Library 5.0 一起使用。请参阅 NHibernate.Logging.CommonLoggingCommon.Logging.EntLib50

        我在网上的任何地方都没有找到这个,所以我想即使问题很旧,我也会发布它。这是一个配置文件(您必须添加必要的引用):

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多