Skip to content

Commit 1a9a507

Browse files
Added thread ID to the context properties of log4net as a workaround for LoggingEvent, where the thread name is only ignored when it matches the default name, which was '.NET Thread Pool Worker' in .NET 6. In .NET 8, the new default name is '.NET TP Worker'.
1 parent 02a050e commit 1a9a507

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
using System.Text;
44
using log4net;
55
using log4net.Core;
6+
using System.Threading;
7+
using log4net.Util;
8+
using System.Globalization;
9+
using System.Security;
610
#if NETCORE
711
using GeneXus.Services.Log;
812
using Microsoft.Extensions.Logging;
@@ -176,12 +180,40 @@ public void LogCritical(string msg, params string[] list)
176180
#endif
177181
internal class GXLoggerLog4Net : IGXLogger
178182
{
183+
const string ThreadNameNet8 = ".NET TP Worker";
184+
const string ThreadNameNet6 = ".NET ThreadPool Worker";
185+
const string ThreadId = "threadid";
179186
internal ILog log { get; set; }
180187

181188
internal GXLoggerLog4Net(ILog logInstance)
182189
{
183190
log = logInstance;
184191
}
192+
void SetThreadIdForLogging()
193+
{
194+
#if NETCORE
195+
if (ThreadContext.Properties[ThreadId] == null)
196+
{
197+
string name = Thread.CurrentThread.Name;
198+
if (!string.IsNullOrEmpty(name) && name != ThreadNameNet6 && !name.StartsWith(ThreadNameNet8))
199+
{
200+
ThreadContext.Properties[ThreadId] = name;
201+
}
202+
else
203+
{
204+
try
205+
{
206+
ThreadContext.Properties[ThreadId] = SystemInfo.CurrentThreadId.ToString(NumberFormatInfo.InvariantInfo);
207+
}
208+
catch (SecurityException)
209+
{
210+
log.Debug("Security exception while trying to get current thread ID. Error Ignored. Empty thread name.");
211+
ThreadContext.Properties[ThreadId] = Thread.CurrentThread.GetHashCode().ToString(CultureInfo.InvariantCulture);
212+
}
213+
}
214+
}
215+
#endif
216+
}
185217
public bool IsTraceEnabled { get => TraceEnabled(); }
186218
public bool IsErrorEnabled { get => ErrorEnabled(); }
187219
public bool IsWarningEnabled { get => WarningEnabled(); }
@@ -215,14 +247,17 @@ public bool CriticalEnabled()
215247

216248
public void LogTrace(string value)
217249
{
250+
SetThreadIdForLogging();
218251
log.Logger.Log(MethodBase.GetCurrentMethod().DeclaringType, Level.Trace, value, null);
219252
}
220253
public void LogError(string msg, Exception ex)
221254
{
255+
SetThreadIdForLogging();
222256
log.Error(msg, ex);
223257
}
224258
public void LogError(string msg)
225259
{
260+
SetThreadIdForLogging();
226261
log.Error(msg);
227262
}
228263
public void LogError(string msg, params string[] list)
@@ -234,14 +269,16 @@ public void LogError(string msg, params string[] list)
234269
message.Append(parm);
235270
}
236271

237-
log.Error(message.ToString());
272+
LogError(message.ToString());
238273
}
239274
public void LogWarning(Exception ex, string msg)
240275
{
276+
SetThreadIdForLogging();
241277
log.Warn(msg, ex);
242278
}
243279
public void LogWarning(string msg)
244280
{
281+
SetThreadIdForLogging();
245282
log.Warn(msg);
246283
}
247284
public void LogWarning(string msg, params string[] list)
@@ -253,15 +290,17 @@ public void LogWarning(string msg, params string[] list)
253290
message.Append(parm);
254291
}
255292

256-
log.Warn(message.ToString());
293+
LogWarning(message.ToString());
257294
}
258295
public void LogDebug(string msg)
259296
{
297+
SetThreadIdForLogging();
260298
log.Debug(msg);
261299
}
262300

263301
public void LogDebug(Exception ex, string msg)
264302
{
303+
SetThreadIdForLogging();
265304
log.Debug(msg, ex);
266305
}
267306
public void LogDebug(string msg, params string[] list)
@@ -273,10 +312,11 @@ public void LogDebug(string msg, params string[] list)
273312
message.Append(parm);
274313
}
275314

276-
log.Debug(message.ToString());
315+
LogDebug(message.ToString());
277316
}
278317
public void LogInfo(string msg)
279318
{
319+
SetThreadIdForLogging();
280320
log.Info(msg);
281321
}
282322
public void LogInfo(string msg, params string[] list)
@@ -288,15 +328,17 @@ public void LogInfo(string msg, params string[] list)
288328
message.Append(parm);
289329
}
290330

291-
log.Info(message.ToString());
331+
LogInfo(message.ToString());
292332
}
293333

294334
public void LogCritical(string msg)
295335
{
336+
SetThreadIdForLogging();
296337
log.Fatal(msg);
297338
}
298339
public void LogCritical(Exception ex, string msg)
299340
{
341+
SetThreadIdForLogging();
300342
log.Fatal(msg, ex);
301343
}
302344

@@ -309,7 +351,7 @@ public void LogCritical(string msg, params string[] list)
309351
message.Append(parm);
310352
}
311353

312-
log.Fatal(message.ToString());
354+
LogCritical(message.ToString());
313355
}
314356
}
315357
public static class GXLogging

0 commit comments

Comments
 (0)