Skip to content

Commit 7339ec3

Browse files
authored
Merge pull request #24 from servicetitan/ormlog-debug
No call of OrmLog.DebugXxx in case Debug level logging is not enabled
2 parents 8968d24 + 0d3b0e0 commit 7339ec3

File tree

9 files changed

+86
-34
lines changed

9 files changed

+86
-34
lines changed

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System;
88
using System.Collections.Concurrent;
99
using System.Collections.Generic;
10-
using System.Diagnostics;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using JetBrains.Annotations;
@@ -19,11 +18,10 @@
1918
using Xtensive.Orm.Internals;
2019
using Xtensive.Orm.Internals.Prefetch;
2120
using Xtensive.Orm.Linq;
21+
using Xtensive.Orm.Logging;
2222
using Xtensive.Orm.Model;
2323
using Xtensive.Orm.Providers;
24-
using Xtensive.Orm.Rse.Providers;
2524
using Xtensive.Orm.Upgrade;
26-
using Xtensive.Orm.Upgrade.Model;
2725
using Xtensive.Sql;
2826
using Xtensive.Sql.Info;
2927

@@ -44,6 +42,8 @@ public sealed class Domain : IDisposable, IHasExtensions
4442
private bool isDisposed;
4543
private Session singleConnectionOwner;
4644

45+
private readonly bool isDebugEventLoggingEnabled;
46+
4747
/// <summary>
4848
/// Occurs when new <see cref="Session"/> is open and activated.
4949
/// </summary>
@@ -239,7 +239,9 @@ internal Session OpenSession(SessionConfiguration configuration, bool activate)
239239
ArgumentValidator.EnsureArgumentNotNull(configuration, "configuration");
240240
configuration.Lock(true);
241241

242-
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
242+
if (isDebugEventLoggingEnabled) {
243+
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
244+
}
243245

244246
Session session;
245247

@@ -378,7 +380,9 @@ internal async Task<Session> OpenSessionInternalAsync(SessionConfiguration confi
378380
ArgumentValidator.EnsureArgumentNotNull(configuration, "configuration");
379381
configuration.Lock(true);
380382

381-
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
383+
if (isDebugEventLoggingEnabled) {
384+
OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
385+
}
382386

383387
Session session;
384388
if (SingleConnection!=null) {
@@ -449,6 +453,7 @@ internal Domain(DomainConfiguration configuration, object upgradeContextCookie,
449453
UpgradeContextCookie = upgradeContextCookie;
450454
SingleConnection = singleConnection;
451455
StorageNodeManager = new StorageNodeManager(Handlers);
456+
isDebugEventLoggingEnabled = OrmLog.IsLogged(LogLevel.Debug); // Just to cache this value
452457
}
453458

454459
/// <inheritdoc/>
@@ -459,7 +464,9 @@ public void Dispose()
459464
return;
460465
isDisposed = true;
461466

462-
OrmLog.Debug(Strings.LogDomainIsDisposing);
467+
if (isDebugEventLoggingEnabled) {
468+
OrmLog.Debug(Strings.LogDomainIsDisposing);
469+
}
463470

464471
NotifyDisposing();
465472
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)