如何为每个SessionFactory分别配置log4net? [英] How to configure log4net separately for each SessionFactory?

查看:361
本文介绍了如何为每个SessionFactory分别配置log4net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用两个数据库.显然,每个数据库都有一个会话工厂.共两个.

My application uses two databases. Obviously, there is one session factory for each database; total two.

我已经为NHibernate配置了log4net来发出生成的SQL.但是,这会将两个会话工厂的日志写入单个文件中.我想为每个会话工厂配置不同的日志文件.

I have configured log4net for NHibernate to emit the generated SQL. But, this writes the log in single file for both the session factories. I want to configure different log file for each session factory.

以下是我的代码:

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders();

FileAppender fileAppender = new FileAppender();
fileAppender.Name = "NHFileAppender";
fileAppender.File = logFilePath;
fileAppender.AppendToFile = false;
fileAppender.LockingModel = new FileAppender.MinimalLock();
fileAppender.Layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss}:%m%n%n");
fileAppender.ActivateOptions();

Logger logger = hierarchy.GetLogger("NHibernate.SQL") as Logger;
logger.Additivity = false;
logger.Level = Level.Debug;
logger.AddAppender(fileAppender);

hierarchy.Configured = true;

您可以在上面的代码中看到,没有任何方法可以为每个会话工厂配置不同的日志文件.如果我正确理解了这一点,则应该有一种方法可以将会话工厂链接/分配给记录器,或者将记录器链接到会话工厂.

As you can see in above code, there is no any way to configure different log file for each session factory. If I am understanding this correctly, there should be a way to link/assign session factory to logger OR logger to session factory.

这可能吗?如果是,怎么办?

Is this possible? If yes, how?

推荐答案

作为

在Visual Studio输出窗口中登录NHibernate
在ASP.NET MVC 4中工作时,NHibernate 'show_sql=true'不会将生成的查询记录到Visual Studio的输出窗口中. 我做了一个简单的实现,用于在Visual Studio的输出窗口中记录NHibernate查询,而无需使用第三方记录器.

NHibernate logging in Visual Studio output window
NHibernate 'show_sql=true' does not log the generated queries to the output window of Visual Studio when working in ASP.NET MVC 4. I made a simple implementation for logging NHibernate queries in Visual Studio's output window, without use of third party loggers.

创建一个拦截器类,并重写一个方法 查看原图?

Create an interceptor class, and override one method view sourceprint?

public class NHLogger : EmptyInterceptor, IInterceptor
{
    public override NHibernate.SqlCommand.SqlString 
       OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
    {
        System.Diagnostics.Debug.WriteLine("NHIBERNATE: " +  sql);
        return base.OnPrepareStatement(sql);
    }
}

然后在Factory的OpenSession中创建它的实例.

Then create an instance of it in the OpenSession of the Factory.

 SessionFactory.OpenSession(new NHLogger());

这不完全是我所需要的;但比我已经使用的要好.我仍然愿意寻求更好的解决方案.

This is not exactly what I need; but better than what I was already using. I am still open for better solution.

这篇关于如何为每个SessionFactory分别配置log4net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆