You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by aa...@apache.org on 2023/03/06 04:54:42 UTC

[rocketmq-clients] 01/05: Allow to adjust log configuration

This is an automated email from the ASF dual-hosted git repository.

aaronai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git

commit a376ca5ac4d353381084dad353e3e3f828baddc6
Author: Aaron Ai <ya...@gmail.com>
AuthorDate: Thu Mar 2 17:43:43 2023 +0800

    Allow to adjust log configuration
---
 csharp/rocketmq-client-csharp/Client.cs       | 42 ++++++++++++++++-----------
 csharp/rocketmq-client-csharp/MqLogManager.cs | 31 +++++++++++++++++---
 2 files changed, 52 insertions(+), 21 deletions(-)

diff --git a/csharp/rocketmq-client-csharp/Client.cs b/csharp/rocketmq-client-csharp/Client.cs
index a9bdb705..e78a8651 100644
--- a/csharp/rocketmq-client-csharp/Client.cs
+++ b/csharp/rocketmq-client-csharp/Client.cs
@@ -313,28 +313,36 @@ namespace Org.Apache.Rocketmq
 
         private async Task<TopicRouteData> FetchTopicRoute0(string topic)
         {
-            var request = new Proto::QueryRouteRequest
+            try
             {
-                Topic = new Proto::Resource
+                var request = new Proto::QueryRouteRequest
                 {
-                    Name = topic
-                },
-                Endpoints = Endpoints.ToProtobuf()
-            };
+                    Topic = new Proto::Resource
+                    {
+                        Name = topic
+                    },
+                    Endpoints = Endpoints.ToProtobuf()
+                };
+
+                var invocation =
+                    await ClientManager.QueryRoute(Endpoints, request, ClientConfig.RequestTimeout);
+                var code = invocation.Response.Status.Code;
+                if (!Proto.Code.Ok.Equals(code))
+                {
+                    Logger.Error($"Failed to fetch topic route, clientId={ClientId}, topic={topic}, code={code}, " +
+                                 $"statusMessage={invocation.Response.Status.Message}");
+                }
 
-            var invocation =
-                await ClientManager.QueryRoute(Endpoints, request, ClientConfig.RequestTimeout);
-            var code = invocation.Response.Status.Code;
-            if (!Proto.Code.Ok.Equals(code))
+                StatusChecker.Check(invocation.Response.Status, request, invocation.RequestId);
+
+                var messageQueues = invocation.Response.MessageQueues.ToList();
+                return new TopicRouteData(messageQueues);
+            }
+            catch (Exception e)
             {
-                Logger.Error($"Failed to fetch topic route, clientId={ClientId}, topic={topic}, code={code}, " +
-                             $"statusMessage={invocation.Response.Status.Message}");
+                Logger.Error(e, $"Failed to fetch topic route, clientId={ClientId}, topic={topic}");
+                throw;
             }
-
-            StatusChecker.Check(invocation.Response.Status, request, invocation.RequestId);
-
-            var messageQueues = invocation.Response.MessageQueues.ToList();
-            return new TopicRouteData(messageQueues);
         }
 
         private async void Heartbeat()
diff --git a/csharp/rocketmq-client-csharp/MqLogManager.cs b/csharp/rocketmq-client-csharp/MqLogManager.cs
index 41ebf5c1..1e67bb56 100644
--- a/csharp/rocketmq-client-csharp/MqLogManager.cs
+++ b/csharp/rocketmq-client-csharp/MqLogManager.cs
@@ -35,13 +35,36 @@ namespace Org.Apache.Rocketmq
 
         private static readonly Lazy<LogFactory> LazyInstance = new(BuildLogFactory);
 
+        private const string FileLogLevelKey = "rocketmq.log.level";
+        private const string FileLogLevel = "Info";
+
+        private const string ConsoleAppenderEnabledKey = "mq.consoleAppender.enabled";
+        private const string ConsoleAppenderEnabled = "false";
+        private const string ConsoleAppenderLogLevel = "Off";
+
+
+        private const string FileLogRootKey = "rocketmq.log.root";
+
+        private const string FileMaxIndexKey = "rocketmq.log.file.maxIndex";
+        private const string FileMaxIndex = "10";
+
         private static LogFactory BuildLogFactory()
         {
+            var fileLogLevel = Environment.GetEnvironmentVariable(FileLogLevelKey) ?? FileLogLevel;
+            var consoleAppenderEnabled =
+                Environment.GetEnvironmentVariable(ConsoleAppenderEnabledKey) ?? ConsoleAppenderEnabled;
+            var consoleLogLevel = bool.Parse(consoleAppenderEnabled) ? fileLogLevel : ConsoleAppenderLogLevel;
+            var fileLogRoot = Environment.GetEnvironmentVariable(FileLogRootKey) ??
+                              Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
+            var fileMaxIndexStr = Environment.GetEnvironmentVariable(FileMaxIndexKey) ?? FileMaxIndex;
+            var fileMaxIndex = int.Parse(fileMaxIndexStr);
+
+
             var config = new LoggingConfiguration();
             var fileTarget = new FileTarget();
             fileTarget.Name = "log_file";
             fileTarget.FileName =
-                new SimpleLayout("${specialfolder:folder=UserProfile}/logs/rocketmq/rocketmq-client.log");
+                new SimpleLayout($"{fileLogRoot}/logs/rocketmq/rocketmq-client.log");
             fileTarget.Layout =
                 new SimpleLayout(
                     "${longdate} ${level:uppercase=true:padding=-5} [${processid}] [${threadid}] [${callsite}:${callsite-linenumber}] ${message} ${onexception:${exception:format=ToString,Data}}");
@@ -49,7 +72,7 @@ namespace Org.Apache.Rocketmq
                 new SimpleLayout("${specialfolder:folder=UserProfile}/logs/rocketmq/rocketmq-client.{######}.log");
             fileTarget.ArchiveAboveSize = 67108864;
             fileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
-            fileTarget.MaxArchiveFiles = 10;
+            fileTarget.MaxArchiveFiles = fileMaxIndex;
             fileTarget.ConcurrentWrites = true;
             fileTarget.KeepFileOpen = false;
 
@@ -66,10 +89,10 @@ namespace Org.Apache.Rocketmq
 
             config.AddTarget(consoleTarget);
 
-            var asyncFileRule = new LoggingRule("*", LogLevel.FromString("Debug"), asyncTargetWrapper);
+            var asyncFileRule = new LoggingRule("*", LogLevel.FromString(fileLogLevel), asyncTargetWrapper);
             config.LoggingRules.Add(asyncFileRule);
 
-            var consoleRule = new LoggingRule("*", LogLevel.FromString("Debug"), consoleTarget);
+            var consoleRule = new LoggingRule("*", LogLevel.FromString(consoleLogLevel), consoleTarget);
             config.LoggingRules.Add(consoleRule);
 
             var logFactory = new LogFactory();