我为log4net创建了一个NHibernate项目和设置。但是控制台中没有显示日志输出。我的app.config
是 -
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net debug="true">
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Counter} - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>
我的hibernate.cfg.xml
是 -
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NhibernateTest">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">
User ID=hr;Password=hr;Data Source=localhost/orcl;
</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="generate_statistics">true</property>
</session-factory>
</hibernate-configuration>
我的主要节目是 -
using System;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace NhibernateTest
{
class Program
{
static void Main(string[] args)
{
LoadNHibernateCfg();
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
Queries query = new Queries();
query.Query1();
}
public static void LoadNHibernateCfg()
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Program).Assembly);
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.SetDelimiter(";");
schemaExport.Execute(true, false,false);
}
}
}
Queries.cs
是 -
using System;
using System.Collections;
using log4net;
namespace NhibernateTest
{
class Queries
{
public static ILog logger = LogManager.GetLogger("Queries.cs");
public void Query1()
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
logger.Debug("Writing Debug Message before");
IEnumerable res = session.CreateQuery(
"select t.tagId, t.TagName from Tags t where t.id<=10"
).Enumerable();
foreach (object[] tuple in res)
{
Console.WriteLine(tuple[0] + " " + tuple[1] + " ");
}
logger.Debug("Writing Debug Message after");
transaction.Commit();
}
}
}
}
}
我有什么遗失的吗?此外,我必须在这个Query1()
函数中找出数据库查询时间。如何使用log4net计算时间或者是否有任何NHibernate实用程序?