Skip to content

Commit

Permalink
Let TcpAppender work in .net 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alanthinker committed Nov 19, 2016
1 parent 728df9e commit 1a4478f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/TestLog4net/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Test
using log4net.Repository.Hierarchy;
using System.Diagnostics;
using System.IO;
using System.Threading;

class Program
{
Expand All @@ -24,7 +25,7 @@ class Program


static void Main(string[] args)
{
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("Config/log4net.config"));

////if (!EventLog.SourceExists("log4net"))
Expand Down Expand Up @@ -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);
Expand Down
69 changes: 63 additions & 6 deletions src/TestLog4net/TcpAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -179,7 +177,7 @@ public override void ActivateOptions()
#region Override implementation of AppenderSkeleton

public readonly object dequeueLocker = new object();
private ConcurrentQueue<string> senderLocalQueue = new ConcurrentQueue<string>();
private SimpleConcurrentQueue<string> senderLocalQueue = new SimpleConcurrentQueue<string>();

ManualResetEvent InnerEnqueueProcessor_MRE = new ManualResetEvent(false);
public void InnerEnqueueProcessor()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -412,4 +410,63 @@ protected virtual void InitializeClientConnection()

#endregion Private Instance Fields
}
// Just a simulation of real ConcurrentQueue

class SimpleConcurrentQueue<T>
{
private Queue<T> _queue = new Queue<T>();

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;
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/TestLog4net/TestLog4net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
Expand Down

0 comments on commit 1a4478f

Please sign in to comment.