Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/log4net/Core/LoggingEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,16 @@ public string ThreadName
m_data.ThreadName =
SystemInfo.CurrentThreadId.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
#else
m_data.ThreadName = System.Threading.Thread.CurrentThread.Name;
if (m_data.ThreadName == null || m_data.ThreadName.Length == 0)
// '.NET ThreadPool Worker' appears as a default thread pool name in .NET 6+.
// Prefer the numeric thread ID instead.
string threadName = System.Threading.Thread.CurrentThread.Name;
if (!string.IsNullOrEmpty(threadName) && threadName != ".NET ThreadPool Worker")
{
// The thread name is not available. Therefore we
m_data.ThreadName = threadName;
}
else
{
// The thread name is not available or unsuitable. Therefore we
// go the the AppDomain to get the ID of the
// current thread. (Why don't Threads know their own ID?)
try
Expand All @@ -847,7 +853,7 @@ public string ThreadName
SystemInfo.CurrentThreadId.ToString(System.Globalization.NumberFormatInfo
.InvariantInfo);
}
catch (System.Security.SecurityException)
catch (SecurityException)
{
// This security exception will occur if the caller does not have
// some undefined set of SecurityPermission flags.
Expand Down