IGNITE-4412 fix NLogLoggerTest thread safety issue

This closes #1401


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/bf118aad
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/bf118aad
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/bf118aad

Branch: refs/heads/ignite-gg-11810-1
Commit: bf118aad8d8251144062d97c476fbe5f817d8018
Parents: d2e6007
Author: Sergey Stronchinskiy <sergey.stronchins...@kraftvaerk.com>
Authored: Mon Jan 9 15:36:11 2017 +0300
Committer: Pavel Tupitsyn <ptupit...@apache.org>
Committed: Mon Jan 9 15:36:11 2017 +0300

----------------------------------------------------------------------
 .../Apache.Ignite.Core.Tests.csproj             |  1 +
 .../Log/ConcurrentMemoryTarget.cs               | 73 ++++++++++++++++++++
 .../Log/NLogLoggerTest.cs                       |  5 +-
 3 files changed, 76 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/bf118aad/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj
----------------------------------------------------------------------
diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj
 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj
index 5948593..55adfe4 100644
--- 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj
@@ -77,6 +77,7 @@
     <Compile Include="Collections\ReadOnlyCollectionTest.cs" />
     <Compile Include="Collections\ReadOnlyDictionaryTest.cs" />
     <Compile Include="Common\IgniteGuidTest.cs" />
+    <Compile Include="Log\ConcurrentMemoryTarget.cs" />
     <Compile Include="Log\DefaultLoggerTest.cs" />
     <Compile Include="Log\Log4NetLoggerTest.cs" />
     <Compile Include="Log\NLogLoggerTest.cs" />

http://git-wip-us.apache.org/repos/asf/ignite/blob/bf118aad/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/ConcurrentMemoryTarget.cs
----------------------------------------------------------------------
diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/ConcurrentMemoryTarget.cs
 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/ConcurrentMemoryTarget.cs
new file mode 100644
index 0000000..66bdbe2
--- /dev/null
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/ConcurrentMemoryTarget.cs
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace Apache.Ignite.Core.Tests.Log
+{
+    using System.Collections.Generic;
+    using System.Linq;
+    using global::NLog;
+    using global::NLog.Targets;
+
+    /// <summary>
+    /// NLog target which supports logging from multiple threads.
+    /// </summary>
+    public class ConcurrentMemoryTarget : TargetWithLayout
+    {
+        /// <summary> Object used for locking. </summary>
+        private readonly object _locker = new object();
+
+        /// <summary> Logs. </summary>
+        private readonly IList<string> _logs;
+
+        /// <summary>
+        /// Initializes a new instance of the <see 
cref="ConcurrentMemoryTarget" /> class.
+        /// </summary>
+        public ConcurrentMemoryTarget()
+        {
+            _logs = new List<string>();
+            Name = "ConcurrentMemoryTarget";
+        }
+
+        /// <summary>
+        /// Gets the collection of logs gathered in the <see 
cref="ConcurrentMemoryTarget" />.
+        /// </summary>
+        public IEnumerable<string> Logs
+        {
+            get
+            {
+                lock (_locker)
+                {
+                    return _logs.ToList();
+                }
+            }
+        }
+
+        /// <summary>
+        /// Renders the logging event message and adds it to the internal 
ArrayList of log messages.
+        /// </summary>
+        /// <param name="logEvent">The logging event.</param>
+        protected override void Write(LogEventInfo logEvent)
+        {
+            lock (_locker)
+            {
+                var msg = Layout.Render(logEvent);
+
+                _logs.Add(msg);
+            }
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/bf118aad/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/NLogLoggerTest.cs
----------------------------------------------------------------------
diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/NLogLoggerTest.cs 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/NLogLoggerTest.cs
index 7806ecd..2743353 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/NLogLoggerTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Log/NLogLoggerTest.cs
@@ -25,7 +25,6 @@ namespace Apache.Ignite.Core.Tests.Log
     using global::NLog;
     using global::NLog.Config;
     using global::NLog.Layouts;
-    using global::NLog.Targets;
     using NUnit.Framework;
     using LogLevel = Apache.Ignite.Core.Log.LogLevel;
 
@@ -35,7 +34,7 @@ namespace Apache.Ignite.Core.Tests.Log
     public class NLogLoggerTest
     {
         /** */
-        private MemoryTarget _logTarget;
+        private ConcurrentMemoryTarget _logTarget;
 
         /// <summary>
         /// Test set up.
@@ -45,7 +44,7 @@ namespace Apache.Ignite.Core.Tests.Log
         {
             var cfg = new LoggingConfiguration();
 
-            _logTarget = new MemoryTarget("mem")
+            _logTarget = new ConcurrentMemoryTarget
             {
                 Layout = new 
SimpleLayout("${Logger}|${Level}|${Message}|${exception}|${all-event-properties}")
             };

Reply via email to