Alright everyone, I am at a lost here. I am working with log4net at my work, and have come to learn log4net can pipe its data to a telnet stream. I have setup the config for the log4net like so
<appender name="RollingTelnetAppender" type="log4net.Appender.telnetappender">
<port value="1200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date Session=%-10property{SessionId} - %message%newline%exception" />
</layout>
</appender>
And then I set this up down in one of my logging nodes
<appender-ref ref="RollingTelnetAppender" />
I am pretty sure I set it up correctly because when I set this all up and try to connect to port 1200 I get the following error
An exception of type 'System.Net.Sockets.SocketException' occurred in System.dll but was not handled in user code
Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted
This is rather frustrating as I can't figure out how to listen to this port for incoming data. This is the code I wrote that refuses to work
public event TCPTalkerResponseHandler ListenerMessage;
private int Port; //set by a constructor to 1200
private TcpListener ServerSocket;
public void Listening ()
{
if (ServerSocket == null)
{
ServerSocket = new TcpListener(IPAddress.Any, Port);
}
ServerSocket.Start();
while (true)
{
if (ServerSocket.Pending())
{
Socket ClientConnection = null;
int NumberOfBytesRead = 0;
byte [] ReadBuffer = new byte [1024];
StringBuilder builder = new StringBuilder();
try
{
ClientConnection = ServerSocket.AcceptSocket();
ClientConnection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, 1);
ClientConnection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
ClientConnection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30000);
do
{
NumberOfBytesRead = ClientConnection.Receive(ReadBuffer, SocketFlags.None);
builder.Append(Encoding.UTF8.GetString(ReadBuffer, 0, NumberOfBytesRead));
}
while (ClientConnection.Available > 0);
if (ListenerMessage != null)
{
ListenerMessage(builder.ToString());
}
}
catch (Exception error)
{
throw new Exception(error.ToString());
}
finally
{
if (ClientConnection != null)
{
ClientConnection.Close();
}
}
}
}
}
Everytime I try triggering the Start piece I get the error you see. So my question is, how the heck to I connect to a telnet stream. If log4net is transmitting over a port, how can I listen to the port and read the data (I really want this to work and not have to do a File watcher looking for changes).