You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by li...@apache.org on 2022/02/21 07:40:28 UTC

[rocketmq-client-csharp] branch develop updated: Log when something is wrong (#11)

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

lizhanhui pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-csharp.git


The following commit(s) were added to refs/heads/develop by this push:
     new c277ead  Log when something is wrong (#11)
c277ead is described below

commit c277ead493bb962ac8630cfb6f5c1d16fb828312
Author: Zhanhui Li <li...@apache.org>
AuthorDate: Mon Feb 21 15:40:23 2022 +0800

    Log when something is wrong (#11)
---
 rocketmq-client-csharp/Client.cs                  | 11 ++++++++---
 rocketmq-client-csharp/ClientLoggerInterceptor.cs | 20 +++++++-------------
 rocketmq-client-csharp/Producer.cs                |  6 ++++++
 3 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/rocketmq-client-csharp/Client.cs b/rocketmq-client-csharp/Client.cs
index 78e2f44..1157bfe 100644
--- a/rocketmq-client-csharp/Client.cs
+++ b/rocketmq-client-csharp/Client.cs
@@ -22,12 +22,14 @@ using System.Threading;
 using System;
 using rmq = apache.rocketmq.v1;
 using grpc = global::Grpc.Core;
+using NLog;
 
 
 namespace org.apache.rocketmq
 {
     public abstract class Client : ClientConfig, IClient
     {
+        private static readonly Logger Logger = MqLogManager.Instance.GetCurrentClassLogger();
 
         public Client(INameServerResolver resolver, string resourceNamespace)
         {
@@ -57,6 +59,7 @@ namespace org.apache.rocketmq
 
         public virtual async Task Shutdown()
         {
+            Logger.Info($"Shutdown client[resource-namespace={resourceNamespace_}");
             updateTopicRouteCTS.Cancel();
             nameServerResolverCTS.Cancel();
             await clientManager.Shutdown();
@@ -68,11 +71,13 @@ namespace org.apache.rocketmq
             if (0 == nameServers.Count)
             {
                 // Whoops, something should be wrong. We got an empty name server list.
+                Logger.Warn("Got an empty name server list");
                 return;
             }
 
             if (nameServers.Equals(this.nameServers))
             {
+                Logger.Debug("Name server list remains unchanged");
                 return;
             }
 
@@ -93,7 +98,7 @@ namespace org.apache.rocketmq
                 }
                 else
                 {
-                    // TODO: log warning here.
+                    Logger.Error("Failed to resolve name server list");
                     return;
                 }
             }
@@ -171,7 +176,7 @@ namespace org.apache.rocketmq
                 }
                 else
                 {
-                    // TODO: log warning here.
+                    Logger.Error("Name server is not properly configured. List is null or empty");
                     return null;
                 }
             }
@@ -273,6 +278,6 @@ namespace org.apache.rocketmq
         private ConcurrentDictionary<string, TopicRouteData> topicRouteTable;
         private CancellationTokenSource updateTopicRouteCTS;
 
-        private const int MaxTransparentRetry = 3;
+        protected const int MaxTransparentRetry = 3;
     }
 }
\ No newline at end of file
diff --git a/rocketmq-client-csharp/ClientLoggerInterceptor.cs b/rocketmq-client-csharp/ClientLoggerInterceptor.cs
index 59ec0f2..8bc98ac 100644
--- a/rocketmq-client-csharp/ClientLoggerInterceptor.cs
+++ b/rocketmq-client-csharp/ClientLoggerInterceptor.cs
@@ -18,11 +18,15 @@ using System;
 using System.Threading.Tasks;
 using Grpc.Core;
 using Grpc.Core.Interceptors;
+using NLog;
 
 namespace org.apache.rocketmq
 {
     public class ClientLoggerInterceptor : Interceptor
     {
+
+        private static readonly Logger Logger = MqLogManager.Instance.GetCurrentClassLogger();
+
         public override TResponse BlockingUnaryCall<TRequest, TResponse>(
             TRequest request,
             ClientInterceptorContext<TRequest, TResponse> context,
@@ -52,19 +56,12 @@ namespace org.apache.rocketmq
             try
             {
                 var response = await t;
-                Console.WriteLine($"Response received: {response}");
+                Logger.Debug($"Response received: {response}");
                 return response;
             }
             catch (Exception ex)
             {
-                // Log error to the console.
-                // Note: Configuring .NET Core logging is the recommended way to log errors
-                // https://docs.microsoft.com/aspnet/core/grpc/diagnostics#grpc-client-logging
-                var initialColor = Console.ForegroundColor;
-                Console.ForegroundColor = ConsoleColor.Red;
-                Console.WriteLine($"Call error: {ex.Message}");
-                Console.ForegroundColor = initialColor;
-
+                Logger.Error($"Call error: {ex.Message}");
                 throw;
             }
         }
@@ -104,10 +101,7 @@ namespace org.apache.rocketmq
             where TRequest : class
             where TResponse : class
         {
-            var initialColor = Console.ForegroundColor;
-            Console.ForegroundColor = ConsoleColor.Green;
-            Console.WriteLine($"Starting call. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}");
-            Console.ForegroundColor = initialColor;
+            Logger.Debug($"Starting call. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}");
         }
 
         private void AddCallerMetadata<TRequest, TResponse>(ref ClientInterceptorContext<TRequest, TResponse> context)
diff --git a/rocketmq-client-csharp/Producer.cs b/rocketmq-client-csharp/Producer.cs
index ab5f48d..ca64b16 100644
--- a/rocketmq-client-csharp/Producer.cs
+++ b/rocketmq-client-csharp/Producer.cs
@@ -21,6 +21,7 @@ using pb = global::Google.Protobuf;
 using grpc = global::Grpc.Core;
 using System.Collections.Generic;
 using System.Collections.Concurrent;
+using NLog;
 
 
 namespace org.apache.rocketmq
@@ -56,6 +57,7 @@ namespace org.apache.rocketmq
                 var topicRouteData = await getRouteFor(message.Topic, false);
                 if (null == topicRouteData || null == topicRouteData.Partitions || 0 == topicRouteData.Partitions.Count)
                 {
+                    Logger.Error($"Failed to resolve route info for {message.Topic} after {MaxTransparentRetry} attempts");
                     throw new TopicRouteException(string.Format("No topic route for {0}", message.Topic));
                 }
 
@@ -119,18 +121,22 @@ namespace org.apache.rocketmq
                 }
                 catch (Exception e)
                 {
+                    Logger.Info(e, $"Failed to send message to {target}");
                     ex = e;
                 }
             }
 
             if (null != ex)
             {
+                Logger.Error(ex, $"Failed to send message after {message.MaxAttemptTimes} attempts");
                 throw ex;
             }
 
+            Logger.Error($"Failed to send message after {message.MaxAttemptTimes} attempts with unspecified reasons");
             throw new Exception("Send message failed");
         }
 
         private ConcurrentDictionary<string, PublishLoadBalancer> loadBalancer;
+        private static readonly Logger Logger = MqLogManager.Instance.GetCurrentClassLogger();
     }
 }
\ No newline at end of file