From a17282a45509cd8d23695ded3938958cfe21ccd0 Mon Sep 17 00:00:00 2001 From: Erik Mavrinac Date: Wed, 1 Dec 2021 11:29:29 -0800 Subject: [PATCH] For %thread/%t, use the numeric thread ID for .NET worker pool threads --- src/log4net/Core/LoggingEvent.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/log4net/Core/LoggingEvent.cs b/src/log4net/Core/LoggingEvent.cs index 4ad16f14..678e62ec 100644 --- a/src/log4net/Core/LoggingEvent.cs +++ b/src/log4net/Core/LoggingEvent.cs @@ -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 @@ -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.