Apply nlog as logging system for rocketmq-client (#6)

diff --git a/rocketmq-client-csharp/MqLogManager.cs b/rocketmq-client-csharp/MqLogManager.cs
new file mode 100644
index 0000000..9e0875b
--- /dev/null
+++ b/rocketmq-client-csharp/MqLogManager.cs
@@ -0,0 +1,30 @@
+using System;
+using System.IO;
+using System.Reflection;
+using NLog;
+using NLog.Config;
+
+namespace org.apache.rocketmq
+{
+    public class MqLogManager
+    {
+        public static LogFactory Instance
+        {
+            get { return LazyInstance.Value; }
+        }
+
+        private static readonly Lazy<LogFactory> LazyInstance = new(BuildLogFactory);
+
+        private static LogFactory BuildLogFactory()
+        {
+            // Use name of current assembly to construct NLog config filename 
+            Assembly thisAssembly = Assembly.GetExecutingAssembly();
+            Console.WriteLine(thisAssembly.Location);
+            string configFilePath = Path.ChangeExtension(thisAssembly.Location, ".nlog");
+
+            LogFactory logFactory = new LogFactory();
+            logFactory.Configuration = new XmlLoggingConfiguration(configFilePath, logFactory);
+            return logFactory;
+        }
+    }
+}
\ No newline at end of file
diff --git a/rocketmq-client-csharp/rocketmq-client-csharp.csproj b/rocketmq-client-csharp/rocketmq-client-csharp.csproj
index eb8149c..24cc710 100644
--- a/rocketmq-client-csharp/rocketmq-client-csharp.csproj
+++ b/rocketmq-client-csharp/rocketmq-client-csharp.csproj
@@ -17,6 +17,7 @@
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

       <PrivateAssets>all</PrivateAssets>

     </PackageReference>

+    <PackageReference Include="NLog" Version="4.7.13" />

 

     <Protobuf Include="Protos\apache\rocketmq\v1\definition.proto" ProtoRoot="Protos" GrpcServices="Client" />

     <Protobuf Include="Protos\google\rpc\code.proto" ProtoRoot="Protos" GrpcServices="Client" />

@@ -29,4 +30,10 @@
     </Protobuf>

   </ItemGroup>

 

+  <ItemGroup>

+    <None Update="rocketmq-client-csharp.nlog">

+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>

+    </None>

+  </ItemGroup>

+

 </Project>

diff --git a/rocketmq-client-csharp/rocketmq-client-csharp.nlog b/rocketmq-client-csharp/rocketmq-client-csharp.nlog
new file mode 100644
index 0000000..b10da72
--- /dev/null
+++ b/rocketmq-client-csharp/rocketmq-client-csharp.nlog
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
+      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+      throwconfigexceptions="true">
+    <targets>
+        <target name="asyncFile" xsi:type="AsyncWrapper">
+            <target name="log_file" xsi:type="File"
+                    fileName="${specialfolder:folder=UserProfile}/logs/ons/ons-client.log"
+                    layout="${longdate} ${level:uppercase=true:padding=-5} [${processid}] [${threadid}] [${callsite}:${callsite-linenumber}] ${message} ${onexception:${exception:format=ToString,Data}}"
+                    archiveFileName="${specialfolder:folder=UserProfile}/logs/ons/ons-client.{######}.log"
+                    archiveAboveSize="67108864"
+                    archiveNumbering="DateAndSequence"
+                    maxArchiveFiles="10"
+                    concurrentWrites="true"
+                    keepFileOpen="false" />
+        </target>
+        <target name="colorConsole" xsi:type="ColoredConsole"
+                useDefaultRowHighlightingRules="true"
+                layout="${longdate} ${level:uppercase=true:padding=-5} [${processid}] [${threadid}] [${callsite}:${callsite-linenumber}] ${message} ${onexception:${exception:format=ToString,Data}}" >
+        </target>
+    </targets>
+    <rules>
+        <logger name="*" writeTo="asyncFile" />
+        <logger name="*" writeTo="colorConsole" />
+    </rules>
+</nlog>
\ No newline at end of file
diff --git a/tests/MqLogManagerTest.cs b/tests/MqLogManagerTest.cs
new file mode 100644
index 0000000..932aa8a
--- /dev/null
+++ b/tests/MqLogManagerTest.cs
@@ -0,0 +1,27 @@
+using System;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using NLog;
+using org.apache.rocketmq;
+
+namespace tests
+{
+    [TestClass]
+    public class MqLogManagerTest
+    {
+        private static readonly Logger Logger = MqLogManager.Instance.GetCurrentClassLogger();
+
+        [TestMethod]
+        public void TestLog()
+        {
+            Logger.Trace("This is a trace message.");
+            Logger.Debug("This is a debug message.");
+            Logger.Info("This is a info message.");
+            Logger.Warn("This is a warn message.");
+            Logger.Error("This is a error message.");
+            Logger.Fatal("This is a fatal message.");
+
+            Logger.Error(new Exception("foobar"), "this is a error message with exception.");
+            Logger.Fatal(new Exception("foobar"), "this is a fatal message with exception.");
+        }
+    }
+}
\ No newline at end of file