From 1a4478fe041a70e3ce38abb0ff2eead95d3136a2 Mon Sep 17 00:00:00 2001 From: AlanThinker Date: Sat, 19 Nov 2016 12:25:27 +0800 Subject: [PATCH] Let TcpAppender work in .net 2.0 --- src/TestLog4net/Program.cs | 17 +++++++- src/TestLog4net/TcpAppender.cs | 69 +++++++++++++++++++++++++++--- src/TestLog4net/TestLog4net.csproj | 2 +- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/TestLog4net/Program.cs b/src/TestLog4net/Program.cs index a11d79f..5d71f73 100644 --- a/src/TestLog4net/Program.cs +++ b/src/TestLog4net/Program.cs @@ -15,6 +15,7 @@ namespace Test using log4net.Repository.Hierarchy; using System.Diagnostics; using System.IO; + using System.Threading; class Program { @@ -24,7 +25,7 @@ class Program static void Main(string[] args) - { + { log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("Config/log4net.config")); ////if (!EventLog.SourceExists("log4net")) @@ -89,6 +90,20 @@ static void DoLog(char keyChar) _log.Info(i); } } + else if (Char.ToLower(keyChar) == 't') + { + for (int i = 0; i < 10; i++) + { + var log = LogManager.GetLogger("Test.Program.t" + i); + ThreadPool.QueueUserWorkItem(delegate (object ob) + { + for (int j = 0; j < 10000; j++) + { + (ob as ILog).Info(j); + } + }, log); + } + } else if (keyChar >= '1' && keyChar <= '9') { _log.Info(keyChar); diff --git a/src/TestLog4net/TcpAppender.cs b/src/TestLog4net/TcpAppender.cs index cc18637..97f48ff 100644 --- a/src/TestLog4net/TcpAppender.cs +++ b/src/TestLog4net/TcpAppender.cs @@ -3,14 +3,12 @@ using System.Net; using System.Net.Sockets; using System.Text; - using log4net.Layout; using log4net.Core; using log4net.Util; using log4net.Appender; using System.Threading; using System.Collections.Generic; -using System.Collections.Concurrent; using System.Diagnostics; namespace AlanThinker.MyLog4net @@ -179,7 +177,7 @@ public override void ActivateOptions() #region Override implementation of AppenderSkeleton public readonly object dequeueLocker = new object(); - private ConcurrentQueue senderLocalQueue = new ConcurrentQueue(); + private SimpleConcurrentQueue senderLocalQueue = new SimpleConcurrentQueue(); ManualResetEvent InnerEnqueueProcessor_MRE = new ManualResetEvent(false); public void InnerEnqueueProcessor() @@ -298,7 +296,7 @@ private void Args_Completed(object sender, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { - + } // In spite of Success or not. Stop waiting. @@ -362,8 +360,8 @@ protected virtual void InitializeClientConnection() { if (this.Client != null) { - this.Client.Dispose(); - } + (this.Client as IDisposable).Dispose(); + } this.Client = new Socket(RemoteAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); this.Client.SendTimeout = 5 * 1000; @@ -412,4 +410,63 @@ protected virtual void InitializeClientConnection() #endregion Private Instance Fields } + // Just a simulation of real ConcurrentQueue + + class SimpleConcurrentQueue + { + private Queue _queue = new Queue(); + + public int Count + { + get + { + lock (_queue) + { + return _queue.Count; + } + } + } + + public void Enqueue(T item) + { + lock (_queue) + { + _queue.Enqueue(item); + } + } + + public bool TryPeek(out T item) + { + lock (_queue) + { + if (_queue.Count > 0) + { + item = _queue.Peek(); + return true; + } + else + { + item = default(T); + return false; + } + } + } + + public bool TryDequeue(out T item) + { + lock (_queue) + { + if (_queue.Count > 0) + { + item = _queue.Dequeue(); + return true; + } + else + { + item = default(T); + return false; + } + } + } + } } \ No newline at end of file diff --git a/src/TestLog4net/TestLog4net.csproj b/src/TestLog4net/TestLog4net.csproj index 72c7fcd..897dd85 100644 --- a/src/TestLog4net/TestLog4net.csproj +++ b/src/TestLog4net/TestLog4net.csproj @@ -41,7 +41,7 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG prompt 4 AllRules.ruleset