问题
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?
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/54478750/nhibernate-fluent-mapping-nlog-no-mapped-documents-found-in-assembly