【问题标题】:How to configure NHibernate logging with log4net in code, not in xml file?如何在代码中而不是在 xml 文件中使用 log4net 配置 NHibernate 日志记录?
【发布时间】:2012-03-28 08:38:11
【问题描述】:

依靠此文档http://nhibernate.info/doc/howto/various/configure-log4net-for-use-with-nhibernate.html,使用 XML 配置文件通过 log4net 配置 NHibernate 日志记录非常容易。

但我需要在 C# 代码中做同样的事情。

【问题讨论】:

    标签: c# nhibernate configuration log4net app-config


    【解决方案1】:

    此答案基于How to configure log4net programmatically from scratch (no config)。但是,由于该答案仅为根记录器提供了解决方案,因此我对其进行了一些扩展。

    /// <summary>
    /// Test for Log4Net
    /// </summary>
    public static void TestLog4Net()
    {
        // Configures log4net
        ConfigureLog4net();
        ILog log = LogManager.GetLogger("foo");
        log.Debug("This should not appear in a logfile!");
    
        ILog log2 = LogManager.GetLogger("NHibernate.SQL");
        log2.Debug("This should only appear in the NH logfile!");
    
        ILog log3 = LogManager.GetLogger("MyProgram");
        log3.Debug("This should appear in the main program logfile!");
    }
    
    /// <summary>
    /// Configures log4net
    /// </summary>
    public static void ConfigureLog4net()
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        // Remove any other appenders
        hierarchy.Root.RemoveAllAppenders();
        // define some basic settings for the root
        Logger rootLogger = hierarchy.Root;
        rootLogger.Level = Level.Debug;
    
        // declare a RollingFileAppender with 5MB per file and max. 10 files
        RollingFileAppender appenderNH = new RollingFileAppender();
        appenderNH.Name = "RollingLogFileAppenderNHibernate";
        appenderNH.AppendToFile = true;
        appenderNH.MaximumFileSize = "5MB";
        appenderNH.MaxSizeRollBackups = 10;
        appenderNH.RollingStyle = RollingFileAppender.RollingMode.Size;
        appenderNH.StaticLogFileName = true;
        appenderNH.LockingModel = new FileAppender.MinimalLock();
        appenderNH.File = "log-nhibernate.log";
        appenderNH.Layout = new PatternLayout("%date - %message%newline");
        // this activates the FileAppender (without it, nothing would be written)
        appenderNH.ActivateOptions();
    
        // This is required, so that we can access the Logger by using 
        // LogManager.GetLogger("NHibernate.SQL") and it can used by NHibernate
        Logger loggerNH = hierarchy.GetLogger("NHibernate.SQL") as Logger;
        loggerNH.Level = Level.Debug;
        loggerNH.AddAppender(appenderNH);
    
        // declare RollingFileAppender with 5MB per file and max. 10 files
        RollingFileAppender appenderMain = new RollingFileAppender();
        appenderMain.Name = "RollingLogFileAppenderMyProgram";
        appenderMain.AppendToFile = true;
        appenderMain.MaximumFileSize = "5MB";
        appenderMain.MaxSizeRollBackups = 10;
        appenderMain.RollingStyle = RollingFileAppender.RollingMode.Size;
        appenderMain.StaticLogFileName = true;
        appenderMain.LockingModel = new FileAppender.MinimalLock();
        appenderMain.File = "log-MyProgram.log";
        appenderMain.Layout = new PatternLayout(
            "%date [%thread] %-5level %logger [%ndc] - %message%newline");
        // this activates the FileAppender (without it, nothing would be written)
        appenderMain.ActivateOptions();
    
        // This is required, so that we can access the Logger by using 
        // LogManager.GetLogger("MyProgram") 
        Logger logger = hierarchy.GetLogger("MyProgram") as Logger;
        logger.Level = Level.Debug;
        logger.AddAppender(appenderMain);
    
        // this is required to tell log4net that we're done 
        // with the configuration, so the logging can start
        hierarchy.Configured = true;
    }
    

    【讨论】:

      【解决方案2】:

      您可以找到有关以编程方式配置 log4net 的信息 herehere。您将需要在项目中直接引用 log4net 程序集,然后编写必要的代码,以便在应用程序启动时执行它(尽可能早)。

      然而问题是你为什么要这样做......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        相关资源
        最近更新 更多