Where to configure NLog NHibernate bridge for logging sql configuration?

穿精又带淫゛_ 提交于 2019-12-24 03:58:08

问题


As a proof-of-concept I am using a console application wrapping a proxy of a WCF service. I execute a few calls through the proxy and write the results to the console. Underlying the WCF service is a Data Access Layer built on the Repository pattern using NHibernate as an ORM. I want to write the NHib-generated SQL to the console.

I have attempted to configure NLog using the NuGet package and guidance from here with no luck, but I suspect perhaps I don't properly understand where I need to perform these different configuration bits in my many-layered architecture. Here's an attempt at describing my layout:

Solution
   |
   -- WCF Service
   |
   -- DAL
   |  |  (NHibernate is used here)
   |  -- hibernate.cfg.xml
   |     NLog.config
   |     App.config (r)
   -- WCF Client (console app)
      |
      --App.config (c)

hibernate.cfg.xml contents:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="show_sql">true</property>
        <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2012;User ID=**;Password=********</property>
    </session-factory>
</hibernate-configuration>

NLog.config contents:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="trace" type="Trace" />
    </targets>
    <rules>
        <logger name="NHibernate.SQL" writeTo="trace" />
    </rules>
</nlog>

App.config (r) contents:

<configuration>
    <appSettings>
        <add key="nhibernate-logger" value="NHibernate.NLogLoggerFactory, NHibernate.NLog" />
    </appSettings>
</configuration>

App.config (c) contents:

<configuration>
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" />
            </listeners>
        </trace>
    </system.diagnostics>
    <appSettings>
        <add key="nhibernate-logger" value="NHibernate.NLogLoggerFactory, NHibernate.NLog" />
    </appSettings>
</configuration>

My NUnit unit tests are displaying the SQL Statements, so the show-sql instruction in hibernate.cfg.xml is working correctly. If I insert a Trace.WriteLine() in my console app that shows up in the console, so (I expect) anything written to Trace ought to show up there. But I do not see any SQL in the console.

I'm assuming that I've placed some of my .config instructions in the wrong layers of my app, but where?

来源:https://stackoverflow.com/questions/17621706/where-to-configure-nlog-nhibernate-bridge-for-logging-sql-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!