Using the Enterprise Library Logging Application Block in NHibernate

We are trying to integrate NHibernate as our OR / M, however we are currently using the Enterprise Library logging application block. I know that NHibernate uses log4net to register. Does anyone have an example of how to use the Enterprise Library to log NHibernate related logs?

+3
source share
4 answers

Write your own log4net appender, which writes to the EL logger. This is an adapter template.

inherits the new / user class appender from 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>

, .

+5

nHibernate log4net? , , log4net EntLibrary.

EntLibrary, Log4Net, nHibernate. log4net , , - .

+1

, . , NHibernate Log4Net, , .

EDIT: NHibernate 3 .

+1

NHibernate 3 and later let you use your Common Logging implementation with Microsoft Enterprise Library 5.0 by simply grabbing the necessary binaries and customizing them. See NHibernate.Logging.CommonLogging with Common.Logging.EntLib50 .

I did not find it anywhere on the network, so I decided that I would publish it, although the question is old. Here is the configuration file (you will need to add the necessary links):

<?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>
+1
source

Source: https://habr.com/ru/post/1705109/


All Articles