Skip to content

Commit 22908b5

Browse files
fredericDelaportehazzik
authored andcommitted
Replacing ThreadLocal by AsyncLocal, and adjusting tests.
1 parent 455b9c8 commit 22908b5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/NHibernate.Test/NHSpecificTest/Logs/LogsFixture.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected override string MappingsAssembly
3131
[Test]
3232
public void WillGetSessionIdFromSessionLogs()
3333
{
34-
ThreadContext.Properties["sessionId"] = new SessionIdCapturer();
34+
GlobalContext.Properties["sessionId"] = new SessionIdCapturer();
3535

3636
using (var spy = new TextLogSpy("NHibernate.SQL", "%message | SessionId: %property{sessionId}"))
3737
using (var s = Sfi.OpenSession())
@@ -45,8 +45,11 @@ public void WillGetSessionIdFromSessionLogs()
4545
}
4646
}
4747

48-
public class SessionIdCapturer
48+
// IFixingRequired interface ensures the value is evaluated at log time rather than at log buffer flush time.
49+
public class SessionIdCapturer : IFixingRequired
4950
{
51+
public object GetFixedObject() => ToString();
52+
5053
public override string ToString()
5154
{
5255
return SessionIdLoggingContext.SessionId.ToString();

src/NHibernate/Impl/SessionIdLoggingContext.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
using System;
2+
using System.Threading;
23

34
namespace NHibernate.Impl
45
{
56
public class SessionIdLoggingContext : IDisposable
67
{
7-
[ThreadStatic]
8-
private static Guid? CurrentSessionId;
8+
private static readonly AsyncLocal<Guid?> _currentSessionId = new AsyncLocal<Guid?>();
99

10-
private readonly Guid? oldSessonId;
10+
private readonly Guid? _oldSessonId;
1111

1212
public SessionIdLoggingContext(Guid id)
1313
{
14-
oldSessonId = SessionId;
14+
_oldSessonId = SessionId;
1515
SessionId = id;
1616
}
1717

1818
/// <summary>
19-
/// We always set the result to use a thread static variable, on the face of it,
19+
/// We always set the result to use an async local variable, on the face of it,
2020
/// it looks like it is not a valid choice, since ASP.Net and WCF may decide to switch
2121
/// threads on us. But, since SessionIdLoggingContext is only used inside NH calls, and since
22-
/// NH calls are never async, this isn't an issue for us.
22+
/// NH calls are either async-await or fully synchronous, this isn't an issue for us.
2323
/// In addition to that, attempting to match to the current context has proven to be performance hit.
2424
/// </summary>
2525
public static Guid? SessionId
2626
{
27-
get { return CurrentSessionId; }
28-
set { CurrentSessionId = value; }
27+
get => _currentSessionId.Value;
28+
set => _currentSessionId.Value = value;
2929
}
3030

3131
#region IDisposable Members
3232

3333
public void Dispose()
3434
{
35-
SessionId = oldSessonId;
35+
SessionId = _oldSessonId;
3636
}
3737

3838
#endregion

0 commit comments

Comments
 (0)