如何使用 Nhibernate-logging 在 NHibernate 3 中仅记录 SQL?

How to log only SQL in NHibernate 3 using Nhibernate-logging?

我正在使用 Nhibernate-logging appsettings 选项来使用我的自定义记录器,实现 IInternalLogger 和 ILoggerFactory,但我只想记录 SQL 句。

public class CustomLogger: IInternalLogger, ILoggerFactory
{
    private TraceSource traceSource;

    public CustomLogger()
    {
        this.traceSource = new TraceSource("CustomLogger");
    }

    public IInternalLogger LoggerFor(Type type)
    {
        return new CustomLogger();
    }

    public IInternalLogger LoggerFor(string keyName)
    {
        return new CustomLogger();
    }

    public void Info(object message)
    {
        this.Log(TraceEventType.Information, message.ToString());
    }

    // etc...
}

"key" 是 LoggerFor(string keyName) 方法中的参数 keyName:

public class LoggerFactory : ILoggerFactory
{
    public LoggerFactory()
    {
    }

    public IInternalLogger LoggerFor(Type type)
    {
        return new AllCustomLogger();
    }

    public IInternalLogger LoggerFor(string keyName)
    {
        if (keyName == "NHibernate.SQL")
        {
            return new OnlySqlCustomLogger();
        }
        else
        {
            return new AllCustomLogger();
        }
    }
}


public class OnlySqlCustomLogger: IInternalLogger
{
    public OnlySqlCustomLogger()
    {
    }

    public void Info(object message)
    {
        this.Log(message.ToString());
    }

// etc...
}

并且在 web.config / app.config:

<add key="nhibernate-logger" value="myNamespace.LoggerFactory, myAssemblyName"/>