Skip to content

Commit 63185dd

Browse files
committed
Changed NH logger implentation, fixed a couple of other issues.
1 parent 1717549 commit 63185dd

File tree

19 files changed

+181
-182
lines changed

19 files changed

+181
-182
lines changed

ConsoleTests/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
3939
<dependentAssembly>
4040
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
41-
<bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
41+
<bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
4242
</dependentAssembly>
4343
<dependentAssembly>
4444
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />

MVC5/MultiTenancyFramework.Mvc.NHibernate/MultiTenancyFramework.Mvc.NHibernate.csproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@
4545
<HintPath>..\..\packages\Antlr3.Runtime.3.5.1\lib\net40-client\Antlr3.Runtime.dll</HintPath>
4646
<Private>True</Private>
4747
</Reference>
48-
<Reference Include="FluentNHibernate, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
49-
<HintPath>..\..\packages\FluentNHibernate.2.1.0\lib\net461\FluentNHibernate.dll</HintPath>
50-
<Private>True</Private>
48+
<Reference Include="FluentNHibernate, Version=2.1.2.0, Culture=neutral, processorArchitecture=MSIL">
49+
<HintPath>..\..\packages\FluentNHibernate.2.1.2\lib\net461\FluentNHibernate.dll</HintPath>
5150
</Reference>
5251
<Reference Include="Iesi.Collections, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
5352
<HintPath>..\..\packages\Iesi.Collections.4.0.4\lib\net461\Iesi.Collections.dll</HintPath>
@@ -61,9 +60,8 @@
6160
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
6261
<Private>True</Private>
6362
</Reference>
64-
<Reference Include="NHibernate, Version=5.1.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
65-
<HintPath>..\..\packages\NHibernate.5.1.0\lib\net461\NHibernate.dll</HintPath>
66-
<Private>True</Private>
63+
<Reference Include="NHibernate, Version=5.2.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
64+
<HintPath>..\..\packages\NHibernate.5.2.1\lib\net461\NHibernate.dll</HintPath>
6765
</Reference>
6866
<Reference Include="Remotion.Linq, Version=2.2.0.0, Culture=neutral, PublicKeyToken=fee00910d6e5f53b, processorArchitecture=MSIL">
6967
<HintPath>..\..\packages\Remotion.Linq.2.2.0\lib\net45\Remotion.Linq.dll</HintPath>

MVC5/MultiTenancyFramework.Mvc.NHibernate/app.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</dependentAssembly>
2525
<dependentAssembly>
2626
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
27-
<bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
27+
<bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
2828
</dependentAssembly>
2929
<dependentAssembly>
3030
<assemblyIdentity name="Iesi.Collections" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Antlr3.Runtime" version="3.5.1" targetFramework="net461" />
4-
<package id="FluentNHibernate" version="2.1.0" targetFramework="net461" />
4+
<package id="FluentNHibernate" version="2.1.2" targetFramework="net461" />
55
<package id="Iesi.Collections" version="4.0.4" targetFramework="net461" />
66
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net461" />
77
<package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net461" />
88
<package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net461" />
99
<package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net461" />
1010
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
11-
<package id="NHibernate" version="5.1.0" targetFramework="net461" />
11+
<package id="NHibernate" version="5.2.1" targetFramework="net461" />
1212
<package id="Remotion.Linq" version="2.2.0" targetFramework="net461" />
1313
<package id="Remotion.Linq.EagerFetching" version="2.2.0" targetFramework="net461" />
1414
</packages>

MultiTenancyFramework.Core/Data/Queries/DbPagingQuery.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
{
33
public abstract class DbPagingQuery
44
{
5+
/// <summary>
6+
/// Think of it as the page number, but here, 0 represents 1st page, etc.
7+
/// </summary>
58
public int PageIndex { get; set; }
9+
/// <summary>
10+
/// How many records to return. Set to a value less than zero inorder to return all
11+
/// </summary>
612
public int PageSize { get; set; }
713
}
814
}

MultiTenancyFramework.Core/Entities/Entity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public override bool Equals(object obj)
7171

7272
public override int GetHashCode()
7373
{
74-
var code = Id.GetHashCode() + base.GetHashCode();
74+
var code = Id.GetHashCode(); // + base.GetHashCode();
7575
if (string.IsNullOrWhiteSpace(InstitutionCode)) code += InstitutionCode.GetHashCode();
7676
return code;
7777
}

MultiTenancyFramework.Core/TaskManager/Tasks/QueuedEmail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class QueuedEmail : BaseEntity
100100
/// </summary>
101101
public virtual long EmailAccountId { get; set; }
102102

103-
public virtual EmailAccount EmailAccount { get; set; }
103+
// public virtual EmailAccount EmailAccount { get; set; }
104104

105105
private EmailAccount _emailAccount;
106106

MultiTenancyFramework.Core/Utility/ILogger.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace MultiTenancyFramework
44
{
55
public interface ILogger
66
{
7+
void LogToDb(bool saveToDb);
8+
79
void SetLogger(object logger);
810

911
void Log(Exception ex, bool isFatal = false);

MultiTenancyFramework.Core/Utility/Logger.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ namespace MultiTenancyFramework
1010
{
1111
public class Logger : ILogger
1212
{
13-
private NLog.Logger _logger = LogManager.GetLogger(ConfigurationHelper.AppSettingsItem<string>("AppName") ?? "MultiTenancyFramework");
14-
private LogLogic logLogic = new LogLogic();
13+
private NLog.Logger _logger;
14+
private bool _logToDb;
15+
public Logger()
16+
{
17+
_logger = LogManager.GetLogger(ConfigurationHelper.AppSettingsItem<string>("AppName") ?? "MultiTenancyFramework");
18+
_logToDb = true;
19+
}
1520

1621
public void SetLogger(object logger)
1722
{
@@ -27,26 +32,6 @@ public void SetLogger(object logger)
2732
}
2833
}
2934

30-
private LogLevel GetLogLevel(LoggingLevel level)
31-
{
32-
switch (level)
33-
{
34-
case LoggingLevel.Debug:
35-
return LogLevel.Debug;
36-
case LoggingLevel.Error:
37-
return LogLevel.Error;
38-
case LoggingLevel.Info:
39-
return LogLevel.Info;
40-
case LoggingLevel.Warn:
41-
return LogLevel.Warn;
42-
case LoggingLevel.Fatal:
43-
return LogLevel.Fatal;
44-
case LoggingLevel.Trace:
45-
default:
46-
return LogLevel.Trace;
47-
}
48-
}
49-
5035
/// <summary>
5136
/// Logs the specified format.
5237
/// </summary>
@@ -79,7 +64,10 @@ public virtual void Log(LoggingLevel level, string format, params object[] args)
7964

8065
_logger.Log(loglevel, format, args);
8166

82-
logLogic.InsertLog(_logger.Name, HttpContext.Current, level, string.Format(format, args));
67+
if (_logToDb)
68+
{
69+
new LogLogic().InsertLog(_logger.Name, HttpContext.Current, level, string.Format(format, args));
70+
}
8371
}
8472
}
8573

@@ -99,8 +87,11 @@ public virtual void Log(Exception ex, bool isFatal = false)
9987

10088
_logger.Log(GetLogLevel(level), exMsg);
10189

102-
logLogic.InsertLog(_logger.Name, context, level, ex.Message, exMsg);
103-
90+
if (_logToDb)
91+
{
92+
new LogLogic().InsertLog(_logger.Name, context, level, ex.Message, exMsg);
93+
}
94+
10495
if (false == doNotSendEmail)
10596
{
10697
try
@@ -135,6 +126,11 @@ public void Info(string format, params object[] args)
135126
Log(LoggingLevel.Info, format, args);
136127
}
137128

129+
public void LogToDb(bool saveToDb)
130+
{
131+
_logToDb = saveToDb;
132+
}
133+
138134
private string BuildErrorMsg(Exception ex, HttpContext context, out bool doNotSendEmail)
139135
{
140136
doNotSendEmail = false;
@@ -209,6 +205,26 @@ private void WriteToFile(string exceptionMessage, HttpContext context, bool isTe
209205
catch { }
210206
}
211207

208+
private LogLevel GetLogLevel(LoggingLevel level)
209+
{
210+
switch (level)
211+
{
212+
case LoggingLevel.Debug:
213+
return LogLevel.Debug;
214+
case LoggingLevel.Error:
215+
return LogLevel.Error;
216+
case LoggingLevel.Info:
217+
return LogLevel.Info;
218+
case LoggingLevel.Warn:
219+
return LogLevel.Warn;
220+
case LoggingLevel.Fatal:
221+
return LogLevel.Fatal;
222+
case LoggingLevel.Trace:
223+
default:
224+
return LogLevel.Trace;
225+
}
226+
}
227+
212228
}
213229

214230
}

MultiTenancyFramework.Core/Utility/SimpleWebHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public virtual void SetCurrentlyLoggedInUser(AppUser user)
7171
/// <exception cref="GeneralException"></exception>
7272
public virtual string GetInstitutionCode(out bool requestAvailable)
7373
{
74-
string core = Utilities.INST_DEFAULT_CODE;
7574
if (IsRequestAvailable(_httpContext))
7675
{
76+
string core = Utilities.INST_DEFAULT_CODE;
7777
requestAvailable = true;
7878
string instCode;
7979
var httpSession = _httpContext.Session;
@@ -82,7 +82,7 @@ public virtual string GetInstitutionCode(out bool requestAvailable)
8282
{
8383
// This can happen when we invoke this before MVC is activated.
8484
#region Pre-MVC
85-
if (routeData.ContainsKey("institution"))
85+
if (routeData != null && routeData.ContainsKey("institution"))
8686
{
8787
return Convert.ToString(routeData["institution"]);
8888
}

MultiTenancyFramework.NHibernate/CoreDAO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void SaveOrUpdate(T obj)
5959

6060
public void TakeOutPermanently(T obj)
6161
{
62-
Delete(obj);
62+
RunDirectQuery(string.Format("DELETE FROM {0} WHERE Id = {1};", obj.GetTableName(), obj.Id));
6363
}
6464

6565
public void Update(T obj)

MultiTenancyFramework.NHibernate/MultiTenancyFramework.NHibernate.csproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@
4545
<HintPath>..\packages\Antlr3.Runtime.3.5.1\lib\net40-client\Antlr3.Runtime.dll</HintPath>
4646
<Private>True</Private>
4747
</Reference>
48-
<Reference Include="FluentNHibernate, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
49-
<HintPath>..\packages\FluentNHibernate.2.1.0\lib\net461\FluentNHibernate.dll</HintPath>
50-
<Private>True</Private>
48+
<Reference Include="FluentNHibernate, Version=2.1.2.0, Culture=neutral, processorArchitecture=MSIL">
49+
<HintPath>..\packages\FluentNHibernate.2.1.2\lib\net461\FluentNHibernate.dll</HintPath>
5150
</Reference>
5251
<Reference Include="Iesi.Collections, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
5352
<HintPath>..\packages\Iesi.Collections.4.0.4\lib\net461\Iesi.Collections.dll</HintPath>
@@ -61,9 +60,8 @@
6160
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
6261
<Private>True</Private>
6362
</Reference>
64-
<Reference Include="NHibernate, Version=5.1.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
65-
<HintPath>..\packages\NHibernate.5.1.0\lib\net461\NHibernate.dll</HintPath>
66-
<Private>True</Private>
63+
<Reference Include="NHibernate, Version=5.2.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
64+
<HintPath>..\packages\NHibernate.5.2.1\lib\net461\NHibernate.dll</HintPath>
6765
</Reference>
6866
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
6967
<HintPath>..\packages\NLog.4.5.1\lib\net45\NLog.dll</HintPath>

MultiTenancyFramework.NHibernate/NHManager/NHSessionManager.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
using NHibernate.Cfg;
1010
using NHibernate.Cfg.ConfigurationSchema;
1111
using NHibernate.Event;
12+
using NHibernate.Engine;
13+
using NHibernate.Type;
1214
using System;
1315
using System.Collections.Concurrent;
1416
using System.Collections.Generic;
1517
using System.Data;
1618
using System.Data.Common;
17-
using System.IO;
1819
using System.Linq;
1920
using System.Reflection;
20-
using System.Runtime.Caching;
2121
using System.Runtime.Remoting.Messaging;
2222
using System.Threading;
2323
using System.Web;
@@ -368,11 +368,17 @@ internal static string GetConnectionString()
368368
return null;
369369
}
370370

371-
internal static ISessionFactory Init(string cfgFile, string sessionKey, IDictionary<string, string> cfgProperties = null)
371+
/// <summary>
372+
///
373+
/// </summary>
374+
/// <param name="cfgFile"></param>
375+
/// <param name="instCode">The modified Institution Code; will be 'Utilities.INST_DEFAULT_CODE' if not tenant</param>
376+
/// <param name="cfgProperties"></param>
377+
/// <returns></returns>
378+
internal static ISessionFactory Init(string cfgFile, string instCode, IDictionary<string, string> cfgProperties = null)
372379
{
373380
Configuration cfg = ConfigureNHibernate(cfgFile, cfgProperties);
374-
375-
string instCode = sessionKey.Split('_')[0];
381+
376382
AutoPersistenceModel autoPersistenceModel = AddMappingAssembliesTo(instCode, cfg);
377383

378384
autoPersistenceModel.Conventions.Add<ClassMappingConvention>();
@@ -406,14 +412,16 @@ internal static ISessionFactory Init(string cfgFile, string sessionKey, IDiction
406412
}
407413
}
408414

415+
private static ILogger logger = Utilities.Logger;
416+
409417
/// <summary>
410418
///
411419
/// </summary>
412420
/// <param name="instCode">The modified Institution Code; will be 'Utilities.INST_DEFAULT_CODE' if not tenant</param>
413421
/// <param name="sessionKey"></param>
414422
private static ISessionFactory BuildFactory(string instCode, string sessionKey)
415423
{
416-
var logger = Utilities.Logger;
424+
logger.LogToDb(false);
417425
logger.Log("Building session factory for {0} with key: {1}...", instCode, sessionKey);
418426
try
419427
{
@@ -425,7 +433,8 @@ private static ISessionFactory BuildFactory(string instCode, string sessionKey)
425433
DatabaseConnection dbConn = null;
426434
if (institution.DatabaseConnectionId > 0)
427435
{
428-
dbConn = new CoreDAO<DatabaseConnection>().Retrieve(institution.DatabaseConnectionId);
436+
dbConn = new CoreDAO<DatabaseConnection>() { InstitutionCode = null }
437+
.Retrieve(institution.DatabaseConnectionId);
429438
}
430439
if (dbConn == null)
431440
{
@@ -450,14 +459,16 @@ private static ISessionFactory BuildFactory(string instCode, string sessionKey)
450459
SessionFactories[instCode] = fac;
451460
return fac;
452461
}
453-
var cfgProps = new Dictionary<string, string>();
454-
cfgProps.Add(Environment.ConnectionString, dbConn.ConnectionString);
462+
var cfgProps = new Dictionary<string, string>
463+
{
464+
{ Environment.ConnectionString, dbConn.ConnectionString }
465+
};
455466

456-
return Init(null, sessionKey, cfgProps);
467+
return Init(null, instCode, cfgProps);
457468
}
458469
else
459470
{
460-
return Init(null, sessionKey);
471+
return Init(null, instCode);
461472
}
462473
}
463474
catch (Exception ex) // I just want to make sure I caught and logged the error
@@ -484,8 +495,10 @@ private static AutoPersistenceModel AddMappingAssembliesTo(string instCode, Conf
484495
throw new HibernateConfigException("Cannot process NHibernate Section in config file.");
485496
}
486497

487-
var mappingAssemblies = new HashSet<string>();
488-
mappingAssemblies.Add(ThisAssembly); // Add yourself
498+
var mappingAssemblies = new HashSet<string>
499+
{
500+
ThisAssembly // Add yourself
501+
};
489502

490503
// Check for automap overrides, ClassMap<> or .hbm type mapping files
491504
if (hc.SessionFactory != null)
@@ -596,16 +609,27 @@ private static Configuration ConfigureNHibernate(string cfgFile, IDictionary<str
596609
cfg.Properties.Remove(Environment.DefaultSchema);
597610
}
598611
#if DEBUG
599-
cfg.Properties.Add(Environment.UseSqlComments, "true");
600-
cfg.Properties.Add(Environment.ShowSql, "true");
601-
cfg.Properties.Add(Environment.FormatSql, "true");
602-
cfg.Properties.Add(Environment.GenerateStatistics, "true");
612+
if (!cfg.Properties.ContainsKey(Environment.UseSqlComments))
613+
cfg.Properties.Add(Environment.UseSqlComments, "true");
614+
if (!cfg.Properties.ContainsKey(Environment.ShowSql))
615+
cfg.Properties.Add(Environment.ShowSql, "true");
616+
if (!cfg.Properties.ContainsKey(Environment.FormatSql))
617+
cfg.Properties.Add(Environment.FormatSql, "true");
618+
if (!cfg.Properties.ContainsKey(Environment.GenerateStatistics))
619+
cfg.Properties.Add(Environment.GenerateStatistics, "true");
603620
#endif
604-
cfg.Properties.Add(Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
605-
cfg.Properties.Add(Environment.Isolation, "ReadCommitted");
606-
cfg.Properties.Add(Environment.ProxyFactoryFactoryClass, "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
607-
cfg.Properties.Add(Environment.CurrentSessionContextClass, "web");
621+
if (!cfg.Properties.ContainsKey(Environment.ConnectionProvider))
622+
cfg.Properties.Add(Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
623+
624+
if (!cfg.Properties.ContainsKey(Environment.Isolation))
625+
cfg.Properties.Add(Environment.Isolation, "ReadCommitted");
626+
627+
if (!cfg.Properties.ContainsKey(Environment.ProxyFactoryFactoryClass))
628+
cfg.Properties.Add(Environment.ProxyFactoryFactoryClass, "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
608629

630+
if (!cfg.Properties.ContainsKey(Environment.CurrentSessionContextClass))
631+
cfg.Properties.Add(Environment.CurrentSessionContextClass, "web");
632+
609633
return cfg;
610634
}
611635

0 commit comments

Comments
 (0)