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).

Recommended Answers

All 2 Replies

Can you tolerate lost data? If so, then you should use rsyslog to a networked stream. If not, log to a local file and have a daemon that uses inotify to learn when the log is updated, and that can stream the data to a remote system. From your code, I assume you are running a Linux/Unix system. If Windows, then all bets are off!

This is Windows.

log4net is already writting to a Telnet stream, I know this. Here's what's really annoying. I fire up Putty, point it to the port, and it is able to read the data (okay that's somewhat a lie, it can partially, it seems to stop, or more it seems log4net stops spitting out data when I close one of the sockets that is another telnet running, and writting data to log4net to spit out to a log)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.