我们正在尝试将NHibernate集成为我们的OR / M,但是,我们目前正在使用Enterprise Library的日志记录应用程序块。我知道NHibernate使用log4net来记录。有没有人有关于如何使用Enterprise Library记录NHibernate相关日志的任何示例?
答案 0 :(得分:5)
编写自己的写入EL记录器的log4net 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>
我没有对此进行测试,但它应该让你去。
答案 1 :(得分:1)
为什么不让nHibernate使用log4net?是的,您必须管理两个,否则您必须为log4net编写适配器以登录到EntLibrary。
我也使用EntLibrary,只需处理Log4Net用于nHibernate。在他们的开发讨论组中,他们谈到了将log4net作为一个depedancy删除,但我认为没有做过任何工作。
答案 2 :(得分:1)
这是我一直在想自己的事情。我可以向你确认NHibernate has a hard dependency on Log4Net,因此,你必须像乔希所说的那样写一个追随者。
编辑:从NHibernate 3开始,不再存在硬依赖。
答案 3 :(得分:1)
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>