Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Logging Abstraction Layer #10

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions StackExchange.NetGain.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using StackExchange.NetGain.WebSockets;
using TcpClient = StackExchange.NetGain.TcpClient;
using System.Text.RegularExpressions;
using StackExchange.NetGain.Logging;

namespace ConsoleApplication1
{
Expand All @@ -19,6 +20,9 @@ class Program

static void Main()
{
// Overriding default NoOpLogManager to log to Console instead
LogManager.Current = new ConsoleLogManager();

IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 6002);
using(var server = new TcpServer())
{
Expand Down
5 changes: 3 additions & 2 deletions StackExchange.NetGain/CircularBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using StackExchange.NetGain.Logging;

namespace StackExchange.NetGain
{
internal class ConnectionSet : IEnumerable<Connection>
{
private Connection[] connections = new Connection[10];
private readonly ILog log = LogManager.Current.GetLogger<ConnectionSet>();

private int count;
public int Count
Expand All @@ -32,7 +33,7 @@ public void Add(Connection connection)
Array.Resize(ref connections, oldLen * 2);
connections[oldLen] = connection;
#if VERBOSE
Debug.WriteLine(string.Format("resized ConnectionSet: {0}", connections.Length));
log.Debug("resized ConnectionSet: {0}", connections.Length);
#endif
}
}
Expand Down
10 changes: 6 additions & 4 deletions StackExchange.NetGain/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
using StackExchange.NetGain.Logging;

namespace StackExchange.NetGain
{
public class Connection
{
public object UserToken { get; set; }

private readonly ILog log;
private IProtocolProcessor protocol;
public Socket Socket { get; set; }
public virtual void Reset()
Expand All @@ -23,6 +24,7 @@ public virtual void Reset()
}
public Connection()
{
log = LogManager.Current.GetLogger<Connection>();
Reset();
}
public void SetProtocol(IProtocolProcessor protocol)
Expand Down Expand Up @@ -203,7 +205,7 @@ public bool ProcessBufferedData(NetContext context, out int messageCount)
if (!CanRead) break; // stop processing data
#if VERBOSE
long inboundLength = incomingBuffer.Length;
Debug.WriteLine(string.Format("[{0}]\tprocessing with {1} bytes available", context.Handler, inboundLength));
log.Debug("[{0}]\tprocessing with {1} bytes available", context.Handler, inboundLength);
#endif
incomingBuffer.Position = 0;
toDiscard = protocol.ProcessIncoming(context, this, incomingBuffer);
Expand All @@ -224,7 +226,7 @@ public bool ProcessBufferedData(NetContext context, out int messageCount)

}
#if VERBOSE
Debug.WriteLine(string.Format("[{0}]\tprocessed {1} bytes; {2} remaining", context.Handler, toDiscard, inboundLength - toDiscard));
log.Debug("[{0}]\tprocessed {1} bytes; {2} remaining", context.Handler, toDiscard, inboundLength - toDiscard);
#endif
} else if(toDiscard < 0)
{
Expand All @@ -240,7 +242,7 @@ public bool ProcessBufferedData(NetContext context, out int messageCount)
else
{
#if VERBOSE
Debug.WriteLine(string.Format("[{0}]\tincomplete", context.Handler));
log.Debug("[{0}]\tincomplete", context.Handler);
#endif
}
} while (toDiscard > 0 && incomingBuffer != null && incomingBuffer.Length > 0);
Expand Down
18 changes: 18 additions & 0 deletions StackExchange.NetGain/Logging/ConsoleLogManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace StackExchange.NetGain.Logging
{
public class ConsoleLogManager : LogManager
{
private static readonly Lazy<ConsoleLogger> Log = new Lazy<ConsoleLogger>(() => new ConsoleLogger());
public override ILog GetLogger<T>()
{
return Log.Value;
}

public override ILog GetLogger(string logName)
{
return Log.Value;
}
}
}
37 changes: 37 additions & 0 deletions StackExchange.NetGain/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace StackExchange.NetGain.Logging
{
public class ConsoleLogger : ILog
{
public void Error(string value)
{
Console.Error.WriteLine(value);
}

public void Error(string format, params object[] arg)
{
Console.Error.WriteLine(format, arg);
}

public void Info(string value)
{
Console.WriteLine(value);
}

public void Info(string format, params object[] arg)
{
Console.WriteLine(format, arg);
}

public void Debug(string value)
{
System.Diagnostics.Debug.WriteLine(value);
}

public void Debug(string format, params object[] arg)
{
System.Diagnostics.Debug.WriteLine(format, arg);
}
}
}
17 changes: 17 additions & 0 deletions StackExchange.NetGain/Logging/ILog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace StackExchange.NetGain.Logging
{
public interface ILog
{
void Error(string value);

void Error(string format, params object[] arg);

void Info(string value);

void Info(string format, params object[] arg);

void Debug(string value);

void Debug(string format, params object[] arg);
}
}
16 changes: 16 additions & 0 deletions StackExchange.NetGain/Logging/LogManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace StackExchange.NetGain.Logging
{
public abstract class LogManager
{
public static LogManager Current { get; set; }

static LogManager()
{
Current = new NoOpLogManager();
}

public abstract ILog GetLogger<T>();

public abstract ILog GetLogger(string logName);
}
}
19 changes: 19 additions & 0 deletions StackExchange.NetGain/Logging/NoOpLogManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace StackExchange.NetGain.Logging
{
public class NoOpLogManager : LogManager
{
private static readonly Lazy<ILog> Log = new Lazy<ILog>(() => new NoOpLogger());

public override ILog GetLogger<T>()
{
return Log.Value;
}

public override ILog GetLogger(string logName)
{
return Log.Value;
}
}
}
16 changes: 16 additions & 0 deletions StackExchange.NetGain/Logging/NoOpLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace StackExchange.NetGain.Logging
{
public class NoOpLogger : ILog {
public void Error(string value) { }

public void Error(string format, params object[] arg) { }

public void Info(string value) { }

public void Info(string format, params object[] arg) { }

public void Debug(string value) { }

public void Debug(string format, params object[] arg) { }
}
}
6 changes: 6 additions & 0 deletions StackExchange.NetGain/StackExchange.NetGain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
<Compile Include="IMessageProcessor.cs" />
<Compile Include="IProtocolFactory.cs" />
<Compile Include="IProtocolProcessor.cs" />
<Compile Include="Logging\ConsoleLogger.cs" />
<Compile Include="Logging\ConsoleLogManager.cs" />
<Compile Include="Logging\ILog.cs" />
<Compile Include="Logging\LogManager.cs" />
<Compile Include="Logging\NoOpLogger.cs" />
<Compile Include="Logging\NoOpLogManager.cs" />
<Compile Include="Message.cs" />
<Compile Include="MessageProcessorServiceInstaller.cs">
<SubType>Component</SubType>
Expand Down
Loading