From 6b2158823eb870c2e1fc150d00ed1e8226a41fe4 Mon Sep 17 00:00:00 2001 From: cmurialdo Date: Tue, 21 Mar 2023 14:26:42 -0300 Subject: [PATCH 1/2] Store threadId property in log4net context to print thread number instead of thread name in log messages. log4net uses thread name for pattern %thread when available, and it uses the thread number if no name is available. In .NET Framework and .NET until v5 threadpool threads are not named by default, therefore thread number is printed in traces by default, but in Net6 they are named. For tracing the thread number is more useful that thread name. --- .../GxClasses/Helpers/GXLogging.cs | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs index c1bdede79..963ed86bf 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs @@ -1,22 +1,26 @@ -using log4net; -using log4net.Core; using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection; using System.Text; -using System.Text.RegularExpressions; +using System.Threading; +using log4net; +using log4net.Core; namespace GeneXus { public static class GXLogging { - + private static void EnsureThreadIdPropertyExistsInContext() + { +#if NETCORE + log4net.ThreadContext.Properties["threadid"] ??= Thread.CurrentThread.ManagedThreadId; +#endif + } public static void Trace(this ILog log, params string[] list) { if (log.Logger.IsEnabledFor(Level.Trace)) { + EnsureThreadIdPropertyExistsInContext(); log.Logger.Log(MethodBase.GetCurrentMethod().DeclaringType, Level.Trace, String.Join(" ", list), null); } } @@ -24,6 +28,7 @@ public static void Error(ILog log, string msg, Exception ex) { if (log.IsErrorEnabled) { + EnsureThreadIdPropertyExistsInContext(); log.Error(msg, ex); } } @@ -32,6 +37,7 @@ public static void ErrorSanitized(ILog log, string msg, Exception ex) { if (log.IsErrorEnabled) { + EnsureThreadIdPropertyExistsInContext(); log.Error(Utils.StringUtil.Sanitize(msg, Utils.StringUtil.LogUserEntryWhiteList), ex); } } @@ -43,6 +49,7 @@ public static void Error(ILog log, string msg1, string msg2, Exception ex) public static void Error(ILog log, Exception ex, params string[] list) { if (log.IsErrorEnabled){ + EnsureThreadIdPropertyExistsInContext(); foreach (string parm in list){ log.Error(parm); } @@ -57,6 +64,7 @@ public static void Warn(ILog log, Exception ex, params string[] list) { if (log.IsWarnEnabled) { + EnsureThreadIdPropertyExistsInContext(); StringBuilder msg = new StringBuilder(); foreach (string parm in list) { @@ -76,6 +84,7 @@ public static void Warn(ILog log, string msg, Exception ex) { if (log.IsWarnEnabled) { + EnsureThreadIdPropertyExistsInContext(); log.Warn(msg, ex); } } @@ -83,6 +92,7 @@ public static void Debug(ILog log, Exception ex, params string[] list) { if (log.IsDebugEnabled) { + EnsureThreadIdPropertyExistsInContext(); StringBuilder msg = new StringBuilder(); foreach (string parm in list) { @@ -99,6 +109,7 @@ public static void DebugSanitized(ILog log, Exception ex, params string[] list) { if (log.IsDebugEnabled) { + EnsureThreadIdPropertyExistsInContext(); StringBuilder msg = new StringBuilder(); foreach (string parm in list) { @@ -127,6 +138,7 @@ public static void Debug(ILog log, string startMsg, Func buildMsg) { if (log.IsDebugEnabled) { + EnsureThreadIdPropertyExistsInContext(); string msg = buildMsg(); log.Debug(startMsg + msg); } @@ -139,6 +151,7 @@ public static void Debug(ILog log, string msg, Exception ex) { if (log.IsDebugEnabled) { + EnsureThreadIdPropertyExistsInContext(); log.Debug(msg, ex); } } @@ -146,6 +159,7 @@ public static void Info(ILog log, params string[] list) { if (log.IsInfoEnabled) { + EnsureThreadIdPropertyExistsInContext(); StringBuilder msg = new StringBuilder(); foreach (string parm in list) { From 70969213a5375ebc77396ba767cd76a252b7e707 Mon Sep 17 00:00:00 2001 From: cmurialdo Date: Tue, 21 Mar 2023 15:00:01 -0300 Subject: [PATCH 2/2] Ensure threadId property In log4net context when using Log API. --- dotnet/src/dotnetframework/GxClasses/Diagnostics/Log.cs | 1 + dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Diagnostics/Log.cs b/dotnet/src/dotnetframework/GxClasses/Diagnostics/Log.cs index 17ede87d5..bc090573a 100644 --- a/dotnet/src/dotnetframework/GxClasses/Diagnostics/Log.cs +++ b/dotnet/src/dotnetframework/GxClasses/Diagnostics/Log.cs @@ -29,6 +29,7 @@ private enum LogLevel private static ILog GetLogger(string topic) { + GXLogging.EnsureThreadIdPropertyExistsInContext(); if (!String.IsNullOrEmpty(topic)) { string loggerName = topic.StartsWith("$") ? topic.Substring(1) : string.Format("{0}.{1}", defaultUserLogNamespace, topic.Trim()); diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs index 963ed86bf..c93a60b9a 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs @@ -10,7 +10,7 @@ namespace GeneXus public static class GXLogging { - private static void EnsureThreadIdPropertyExistsInContext() + internal static void EnsureThreadIdPropertyExistsInContext() { #if NETCORE log4net.ThreadContext.Properties["threadid"] ??= Thread.CurrentThread.ManagedThreadId;