-1

I've successfully used the CommonLogging layer in NHibernate to log its internal messages using NLog for previous projects which were using hbm.xml files. I'm now switching to fluent mapping, and the NHibernate logs now only contain one line:

[Log entry: Warn] 2019-02-01 13:30:42.5537 No mapped documents found in assembly: <assembly name>

I also tried to move the nhibernate-logger configuration directive from the App.config file to the code, just after configuring the mapping – and I'm receiving the same warning as before:

var dbCfg = new Configuration();
dbCfg.Configure();
dbCfg = Fluently.Configure(dbCfg)
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<RetailerMapping>())
    .ExposeConfiguration(c =>
    {
        c.SetProperty(@"nhibernate-logger", @"NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, NHibernate.Logging.CommonLogging");
    })
    .BuildConfiguration();
dbCfg.AddAssembly(Assembly.GetExecutingAssembly().GetName().Name);

What am I doing wrong?

Bogdan Stăncescu
  • 5,320
  • 3
  • 24
  • 25
  • Yes, everything(-ish) apart from logging works as expected. I'm enabling logging because I'm trying to debug an issue I have with some updates on stateless sessions, but selects and inserts work. – Bogdan Stăncescu Feb 01 '19 at 13:14
  • Actually, if you're using CommonLogging then the underlaying logger is irrelevant – so yes, that should help. – Bogdan Stăncescu Feb 01 '19 at 13:32

2 Answers2

1

As discussed in comments, log4net is also suitable to you.

Following is the code that enables logging SQL statements on NHibernate:

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

FileAppender fileAppender = new FileAppender();
fileAppender.Name = "NHFileAppender";
fileAppender.File = config.LogFilePath;
fileAppender.AppendToFile = true;
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;

You can play with FileAppender and Logger class to meet your additional requirements. This Q/A may also help.

I do not understand why you need to input mapping assemblies to logger. As you can see above, it is not needed for log4net configurations. This way hopefully, your issue should be resolved.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
0

Ok, I'm quite embarrassed by this, and I was tempted to delete the question altogether, but I decided to post the solution just in case some other airhead hits the same snag: I had forgotten to remove the minLevel directive from the NLog configuration, and I was only logging warnings, errors and fatal errors – and that warning made me think it wasn't logging because of the warning, when in fact it wasn't logging because I had inhibited lower level messages.

Bogdan Stăncescu
  • 5,320
  • 3
  • 24
  • 25