Skip to content

Commit 6fcc003

Browse files
committed
Do not call OrmLog.DebugXxx in case Debug level logging is not enabled
It is too expensive to get values of Strings.Xxx properties when those are not needed for actual logging
1 parent b4d0472 commit 6fcc003

9 files changed

Lines changed: 86 additions & 31 deletions

File tree

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Xtensive.Orm.Internals;
2020
using Xtensive.Orm.Internals.Prefetch;
2121
using Xtensive.Orm.Linq;
22+
using Xtensive.Orm.Logging;
2223
using Xtensive.Orm.Model;
2324
using Xtensive.Orm.Providers;
2425
using Xtensive.Orm.Rse.Providers;
@@ -44,6 +45,8 @@ public sealed class Domain : IDisposable, IHasExtensions
4445
private bool isDisposed;
4546
private Session singleConnectionOwner;
4647

48+
private bool IsDebugEventLoggingEnabled { get; set; }
49+
4750
/// <summary>
4851
/// Occurs when new <see cref="Session"/> is open and activated.
4952
/// </summary>
@@ -239,7 +242,9 @@ internal Session OpenSession(SessionConfiguration configuration, bool activate)
239242
ArgumentValidator.EnsureArgumentNotNull(configuration, "configuration");
240243
configuration.Lock(true);
241244

242-
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
245+
if (IsDebugEventLoggingEnabled) {
246+
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
247+
}
243248

244249
Session session;
245250

@@ -378,7 +383,9 @@ internal async Task<Session> OpenSessionInternalAsync(SessionConfiguration confi
378383
ArgumentValidator.EnsureArgumentNotNull(configuration, "configuration");
379384
configuration.Lock(true);
380385

381-
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
386+
if (IsDebugEventLoggingEnabled) {
387+
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
388+
}
382389

383390
Session session;
384391
if (SingleConnection!=null) {
@@ -449,6 +456,7 @@ internal Domain(DomainConfiguration configuration, object upgradeContextCookie,
449456
UpgradeContextCookie = upgradeContextCookie;
450457
SingleConnection = singleConnection;
451458
StorageNodeManager = new StorageNodeManager(Handlers);
459+
IsDebugEventLoggingEnabled = OrmLog.IsLogged(LogLevel.Debug); // Just to cache this value
452460
}
453461

454462
/// <inheritdoc/>
@@ -459,7 +467,9 @@ public void Dispose()
459467
return;
460468
isDisposed = true;
461469

462-
OrmLog.Debug(Strings.LogDomainIsDisposing);
470+
if (IsDebugEventLoggingEnabled) {
471+
OrmLog.Debug(Strings.LogDomainIsDisposing);
472+
}
463473

464474
NotifyDisposing();
465475
Services.Dispose();

Orm/Xtensive.Orm/Orm/Entity.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,9 @@ private bool SystemUpdateVersionInfo(Entity changedEntity, FieldInfo changedFiel
482482

483483
internal void SystemBeforeRemove()
484484
{
485-
OrmLog.Debug(Strings.LogSessionXRemovingKeyY, Session, Key);
485+
if (Session.IsDebugEventLoggingEnabled) {
486+
OrmLog.Debug(Strings.LogSessionXRemovingKeyY, Session, Key);
487+
}
486488

487489
Session.SystemEvents.NotifyEntityRemoving(this);
488490
using (Session.Operations.EnableSystemOperationRegistration()) {
@@ -527,7 +529,9 @@ internal void SystemRemoveCompleted(Exception exception)
527529
internal override sealed void SystemBeforeInitialize(bool materialize)
528530
{
529531
State.Entity = this;
530-
OrmLog.Debug(Strings.LogSessionXMaterializingYKeyZ, Session, GetType().GetShortName(), State.Key);
532+
if (Session.IsDebugEventLoggingEnabled) {
533+
OrmLog.Debug(Strings.LogSessionXMaterializingYKeyZ, Session, GetType().GetShortName(), State.Key);
534+
}
531535

532536
if (Session.IsSystemLogicOnly || materialize)
533537
return;
@@ -613,11 +617,9 @@ internal override sealed void SystemBeforeGetValue(FieldInfo field)
613617
{
614618
if (!Session.Configuration.Supports(SessionOptions.ReadRemovedObjects))
615619
EnsureNotRemoved();
616-
617-
// getting of field value is frequent operation and access to resources
618-
// will slow down execution significantly
619-
if (OrmLog.IsLogged(LogLevel.Debug))
620+
if (Session.IsDebugEventLoggingEnabled) {
620621
OrmLog.Debug(Strings.LogSessionXGettingValueKeyYFieldZ, Session, Key, field);
622+
}
621623

622624
EnsureIsFetched(field);
623625

@@ -665,7 +667,9 @@ internal override sealed void SystemSetValueAttempt(FieldInfo field, object valu
665667
{
666668
EnsureNotRemoved();
667669

668-
OrmLog.Debug(Strings.LogSessionXSettingValueKeyYFieldZ, Session, Key, field);
670+
if (Session.IsDebugEventLoggingEnabled) {
671+
OrmLog.Debug(Strings.LogSessionXSettingValueKeyYFieldZ, Session, Key, field);
672+
}
669673

670674
if (field.IsPrimaryKey)
671675
throw new NotSupportedException(string.Format(Strings.ExUnableToSetKeyFieldXExplicitly, field.Name));

Orm/Xtensive.Orm/Orm/Key.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ public TypeInfo ResolveTypeInfo([NotNull, InstantHandle] Session session)
119119
if (IsTemporary(domain))
120120
return TypeReference.Type;
121121

122-
OrmLog.Debug(Strings.LogSessionXResolvingKeyYExactTypeIsUnknownFetchIsRequired, session, this);
122+
if (session.IsDebugEventLoggingEnabled) {
123+
OrmLog.Debug(Strings.LogSessionXResolvingKeyYExactTypeIsUnknownFetchIsRequired, session, this);
124+
}
123125

124126
var entityState = session.Handler.FetchEntityState(this);
125127
if (entityState==null || entityState.IsNotAvailableOrMarkedAsRemoved)

Orm/Xtensive.Orm/Orm/Operations/Internals/OperationRegistrationScope.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public void RegisterEntityIdentifier(Key key, string identifier)
6565
KeyByIdentifier.Add(identifier, key);
6666
IdentifierByKey.Add(key, identifier);
6767

68-
OrmLog.Debug(Strings.LogSessionXEntityWithKeyYIdentifiedAsZ, Owner.Session, key, identifier);
68+
if (Owner.Session.IsDebugEventLoggingEnabled) {
69+
OrmLog.Debug(Strings.LogSessionXEntityWithKeyYIdentifiedAsZ, Owner.Session, key, identifier);
70+
}
6971
}
7072

7173

Orm/Xtensive.Orm/Orm/QueryEndpoint.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ [CanBeNull] public Entity SingleOrDefault(Key key)
287287
using (var tx = session.OpenAutoTransaction()) {
288288
EntityState state;
289289
if (!session.LookupStateInCache(key, out state)) {
290-
OrmLog.Debug(Strings.LogSessionXResolvingKeyYExactTypeIsZ, session, key, key.HasExactType ? Strings.Known : Strings.Unknown);
290+
if (session.IsDebugEventLoggingEnabled) {
291+
OrmLog.Debug(Strings.LogSessionXResolvingKeyYExactTypeIsZ, session, key, key.HasExactType ? Strings.Known : Strings.Unknown);
292+
}
293+
291294
state = session.Handler.FetchEntityState(key);
292295
}
293296
else if (state.Tuple==null) {

Orm/Xtensive.Orm/Orm/Session.Cache.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public partial class Session
2929

3030
internal void Invalidate()
3131
{
32-
OrmLog.Debug(Strings.LogSessionXInvalidate, this);
32+
if (IsDebugEventLoggingEnabled) {
33+
OrmLog.Debug(Strings.LogSessionXInvalidate, this);
34+
}
3335

3436
ClearChangeRegistry();
3537
InvalidateCachedEntities();
@@ -65,7 +67,10 @@ internal void RemapEntityKeys(KeyMapping keyMapping)
6567
Persist(PersistReason.RemapEntityKeys);
6668
Invalidate();
6769
}
68-
OrmLog.Debug(Strings.LogSessionXRemappingEntityKeys, this);
70+
if (IsDebugEventLoggingEnabled) {
71+
OrmLog.Debug(Strings.LogSessionXRemappingEntityKeys, this);
72+
}
73+
6974
foreach (var entityState in EntityChangeRegistry.GetItems(PersistenceState.New)) {
7075
var key = entityState.Key;
7176
var remappedKey = keyMapping.TryRemapKey(key);
@@ -119,8 +124,9 @@ internal void RemoveOrCreateRemovedEntity(Type type, Key key)
119124
};
120125
EntityStateCache.Add(result);
121126

122-
OrmLog.Debug(Strings.LogSessionXCachingY, this, result);
123-
return;
127+
if (IsDebugEventLoggingEnabled) {
128+
OrmLog.Debug(Strings.LogSessionXCachingY, this, result);
129+
}
124130
}
125131

126132
internal void InitializeEntity(Entity entity, bool materialize)
@@ -164,7 +170,10 @@ internal EntityState CreateEntityState(Key key, bool failIfStateIsAlreadyBound)
164170
result.PersistenceState = PersistenceState.New;
165171
}
166172

167-
OrmLog.Debug(Strings.LogSessionXCachingY, this, result);
173+
if (IsDebugEventLoggingEnabled) {
174+
OrmLog.Debug(Strings.LogSessionXCachingY, this, result);
175+
}
176+
168177
return result;
169178
}
170179

@@ -212,7 +221,9 @@ internal EntityState UpdateStateInCache(Key key, Tuple tuple, bool isStale)
212221
}
213222
result.Update(tuple);
214223
result.IsStale = isStale;
215-
OrmLog.Debug(Strings.LogSessionXUpdatingCacheY, this, result);
224+
if (IsDebugEventLoggingEnabled) {
225+
OrmLog.Debug(Strings.LogSessionXUpdatingCacheY, this, result);
226+
}
216227
}
217228
return result;
218229
}
@@ -246,7 +257,10 @@ private EntityState AddEntityStateToCache(Key key, Tuple tuple, bool isStale)
246257
PersistenceState = PersistenceState.Synchronized
247258
};
248259
EntityStateCache.Add(result);
249-
OrmLog.Debug(Strings.LogSessionXCachingY, this, result);
260+
if (IsDebugEventLoggingEnabled) {
261+
OrmLog.Debug(Strings.LogSessionXCachingY, this, result);
262+
}
263+
250264
return result;
251265
}
252266

Orm/Xtensive.Orm/Orm/Session.Persist.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ internal void Persist(PersistReason reason)
8888
try {
8989
using (this.OpenSystemLogicOnlyRegion()) {
9090
DemandTransaction();
91-
OrmLog.Debug(Strings.LogSessionXPersistingReasonY, this, reason);
91+
if (IsDebugEventLoggingEnabled) {
92+
OrmLog.Debug(Strings.LogSessionXPersistingReasonY, this, reason);
93+
}
9294

9395
EntityChangeRegistry itemsToPersist;
9496
if (performPinning) {
@@ -132,7 +134,9 @@ internal void Persist(PersistReason reason)
132134
EntitySetChangeRegistry.Clear();
133135
NonPairedReferencesRegistry.Clear();
134136
}
135-
OrmLog.Debug(Strings.LogSessionXPersistCompleted, this);
137+
if (IsDebugEventLoggingEnabled) {
138+
OrmLog.Debug(Strings.LogSessionXPersistCompleted, this);
139+
}
136140
}
137141
}
138142
SystemEvents.NotifyPersisted();

Orm/Xtensive.Orm/Orm/Session.Transactions.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ internal void BeginTransaction(Transaction transaction)
148148

149149
internal void CommitTransaction(Transaction transaction)
150150
{
151-
OrmLog.Debug(Strings.LogSessionXCommittingTransaction, this);
151+
if (IsDebugEventLoggingEnabled) {
152+
OrmLog.Debug(Strings.LogSessionXCommittingTransaction, this);
153+
}
152154

153155
SystemEvents.NotifyTransactionPrecommitting(transaction);
154156
Events.NotifyTransactionPrecommitting(transaction);
@@ -169,7 +171,10 @@ internal void CommitTransaction(Transaction transaction)
169171
internal void RollbackTransaction(Transaction transaction)
170172
{
171173
try {
172-
OrmLog.Debug(Strings.LogSessionXRollingBackTransaction, this);
174+
if (IsDebugEventLoggingEnabled) {
175+
OrmLog.Debug(Strings.LogSessionXRollingBackTransaction, this);
176+
}
177+
173178
SystemEvents.NotifyTransactionRollbacking(transaction);
174179
Events.NotifyTransactionRollbacking(transaction);
175180
}
@@ -226,12 +231,18 @@ internal void CompleteTransaction(Transaction transaction)
226231

227232
switch (transaction.State) {
228233
case TransactionState.Committed:
229-
OrmLog.Debug(Strings.LogSessionXCommittedTransaction, this);
234+
if (IsDebugEventLoggingEnabled) {
235+
OrmLog.Debug(Strings.LogSessionXCommittedTransaction, this);
236+
}
237+
230238
SystemEvents.NotifyTransactionCommitted(transaction);
231239
Events.NotifyTransactionCommitted(transaction);
232240
break;
233241
case TransactionState.RolledBack:
234-
OrmLog.Debug(Strings.LogSessionXRolledBackTransaction, this);
242+
if (IsDebugEventLoggingEnabled) {
243+
OrmLog.Debug(Strings.LogSessionXRolledBackTransaction, this);
244+
}
245+
235246
SystemEvents.NotifyTransactionRollbacked(transaction);
236247
Events.NotifyTransactionRollbacked(transaction);
237248
break;
@@ -292,18 +303,21 @@ private TransactionScope CreateNestedTransaction(IsolationLevel isolationLevel,
292303

293304
private TransactionScope OpenTransactionScope(Transaction transaction)
294305
{
295-
OrmLog.Debug(Strings.LogSessionXOpeningTransaction, this);
296-
306+
if (IsDebugEventLoggingEnabled) {
307+
OrmLog.Debug(Strings.LogSessionXOpeningTransaction, this);
308+
}
309+
297310
SystemEvents.NotifyTransactionOpening(transaction);
298311
Events.NotifyTransactionOpening(transaction);
299312

300313
Transaction = transaction;
301314
transaction.Begin();
302315

303316
IDisposable logIndentScope = null;
304-
if (IsDebugEventLoggingEnabled)
317+
if (IsDebugEventLoggingEnabled) {
305318
logIndentScope = OrmLog.DebugRegion(Strings.LogSessionXTransaction, this);
306-
319+
}
320+
307321
SystemEvents.NotifyTransactionOpened(transaction);
308322
Events.NotifyTransactionOpened(transaction);
309323

Orm/Xtensive.Orm/Orm/Session.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,9 @@ public void Dispose()
575575
if (isDisposed)
576576
return;
577577
try {
578-
OrmLog.Debug(Strings.LogSessionXDisposing, this);
578+
if (IsDebugEventLoggingEnabled) {
579+
OrmLog.Debug(Strings.LogSessionXDisposing, this);
580+
}
579581

580582
SystemEvents.NotifyDisposing();
581583
Events.NotifyDisposing();

0 commit comments

Comments
 (0)