Skip to content

Commit 0b9f537

Browse files
authored
Merge pull request #78 from erikma/dev/erikmav/net6ThreadName
2 parents 9410325 + a17282a commit 0b9f537

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/log4net/Core/LoggingEvent.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,10 +835,16 @@ public string ThreadName
835835
m_data.ThreadName =
836836
SystemInfo.CurrentThreadId.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
837837
#else
838-
m_data.ThreadName = System.Threading.Thread.CurrentThread.Name;
839-
if (m_data.ThreadName == null || m_data.ThreadName.Length == 0)
838+
// '.NET ThreadPool Worker' appears as a default thread pool name in .NET 6+.
839+
// Prefer the numeric thread ID instead.
840+
string threadName = System.Threading.Thread.CurrentThread.Name;
841+
if (!string.IsNullOrEmpty(threadName) && threadName != ".NET ThreadPool Worker")
840842
{
841-
// The thread name is not available. Therefore we
843+
m_data.ThreadName = threadName;
844+
}
845+
else
846+
{
847+
// The thread name is not available or unsuitable. Therefore we
842848
// go the the AppDomain to get the ID of the
843849
// current thread. (Why don't Threads know their own ID?)
844850
try
@@ -847,7 +853,7 @@ public string ThreadName
847853
SystemInfo.CurrentThreadId.ToString(System.Globalization.NumberFormatInfo
848854
.InvariantInfo);
849855
}
850-
catch (System.Security.SecurityException)
856+
catch (SecurityException)
851857
{
852858
// This security exception will occur if the caller does not have
853859
// some undefined set of SecurityPermission flags.

0 commit comments

Comments
 (0)