我们正在尝试将 NHibernate 集成为我们的 OR/M,但是,我们目前正在使用 Enterprise Library 的日志记录应用程序块。我知道 NHibernate 使用 log4net 来记录。有没有人有任何关于如何使用 Enterprise Library 记录 NHibernate 相关日志的示例?
4 回答
编写你自己的 log4net appender 写入 EL 记录器。这是一个适配器模式。
继承一个新的/自定义的 appender 类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>
我没有测试过这个,但它应该能让你继续前进。
为什么不让 nHibernate 使用 log4net?是的,您必须管理两个,否则您必须为 log4net 编写一个适配器以登录到 EntLibrary。
我也使用 EntLibrary,只处理用于 nHibernate 的 Log4Net。在他们的开发讨论组中,他们谈到删除 log4net 作为一种依赖,但我认为尚未对此进行任何工作。
这是我一直想知道的关于自己的事情。我可以为您确认NHibernate 对 Log4Net 有很强的依赖关系,因此,您必须按照 Josh 的说明编写一个附加程序。
编辑:从 NHibernate 3 开始,不再存在硬依赖。
NHibernate 3 及更高版本允许您通过获取必要的二进制文件并配置它们来使用他们的 Common Logging 实现以及 Microsoft 的 Enterprise Library 5.0。请参阅NHibernate.Logging.CommonLogging和Common.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}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
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 & 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>