1

I'm using Nhibernate-logging appsettings option to use my custom logger, implementing IInternalLogger and ILoggerFactory, but I only want to log SQL sentences.

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...
}

1 Answer 1

1

The "key" is the param keyName in the LoggerFor(string keyName) method:

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...
}

And in the web.config / app.config:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.