Skip to content

Commit e9e2bf0

Browse files
achaplaLightning UnicornMpdreamz
authored
Cache UserName and Domain to improve performance when creating new EcsDocument (#287)
Co-authored-by: Lightning Unicorn <[email protected]> Co-authored-by: Martijn Laarman <[email protected]>
1 parent e2b8208 commit e9e2bf0

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Elastic.CommonSchema/EcsDocument.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public static TEcsDocument CreateNewWithDefaults<TEcsDocument>(
5757

5858
if (options?.IncludeHost is null or true) doc.Host = GetHost();
5959
if (options?.IncludeProcess is null or true) doc.Process = GetProcess();
60-
// TODO I think we can cache user? does CurrentPrincipal on Thread ever change?
61-
if (options?.IncludeUser is null or true)
62-
doc.User = new User { Id = Thread.CurrentPrincipal?.Identity.Name, Name = Environment.UserName, Domain = Environment.UserDomainName };
60+
if (options?.IncludeUser is null or true) doc.User = GetUser();
6361

6462
return doc;
6563
}
@@ -157,6 +155,12 @@ private static Process GetProcess()
157155
};
158156
}
159157

158+
private static readonly string UserName = Environment.UserName;
159+
private static readonly string UserDomainName = Environment.UserDomainName;
160+
161+
//Can not cache current thread's identity as it's used for role based security, different threads can have different identities
162+
private static User GetUser() => new User { Id = Thread.CurrentPrincipal?.Identity.Name, Name = UserName, Domain = UserDomainName };
163+
160164
private static Error GetError(Exception exception)
161165
{
162166
if (exception == null)

0 commit comments

Comments
 (0)