123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- #region Apache License
- #endregion
- using System;
- using System.Collections;
- using System.Globalization;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- using System.Threading;
- #if NETSTANDARD1_3
- using System.Threading.Tasks;
- #endif
- using log4net.Layout;
- using log4net.Core;
- using log4net.Util;
- namespace log4net.Appender
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class TelnetAppender : AppenderSkeleton
- {
- private SocketHandler m_handler;
- private int m_listeningPort = 23;
- #region Constructor
-
-
-
-
-
-
-
-
- public TelnetAppender()
- {
- }
- #endregion
- #region Private Static Fields
-
-
-
-
-
-
-
- private readonly static Type declaringType = typeof(TelnetAppender);
- #endregion Private Static Fields
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int Port
- {
- get
- {
- return m_listeningPort;
- }
- set
- {
- if (value < IPEndPoint.MinPort || value > IPEndPoint.MaxPort)
- {
- throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("value", (object)value,
- "The value specified for Port is less than " +
- IPEndPoint.MinPort.ToString(NumberFormatInfo.InvariantInfo) +
- " or greater than " +
- IPEndPoint.MaxPort.ToString(NumberFormatInfo.InvariantInfo) + ".");
- }
- else
- {
- m_listeningPort = value;
- }
- }
- }
- #region Override implementation of AppenderSkeleton
-
-
-
-
-
-
-
-
- protected override void OnClose()
- {
- base.OnClose();
- if (m_handler != null)
- {
- m_handler.Dispose();
- m_handler = null;
- }
- }
-
-
-
-
-
-
-
-
-
- protected override bool RequiresLayout
- {
- get { return true; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override void ActivateOptions()
- {
- base.ActivateOptions();
- try
- {
- LogLog.Debug(declaringType, "Creating SocketHandler to listen on port ["+m_listeningPort+"]");
- m_handler = new SocketHandler(m_listeningPort);
- }
- catch(Exception ex)
- {
- LogLog.Error(declaringType, "Failed to create SocketHandler", ex);
- throw;
- }
- }
-
-
-
-
-
-
-
-
-
- protected override void Append(LoggingEvent loggingEvent)
- {
- if (m_handler != null && m_handler.HasConnections)
- {
- m_handler.Send(RenderLoggingEvent(loggingEvent));
- }
- }
- #endregion
- #region SocketHandler helper class
-
-
-
-
-
-
-
-
-
-
- protected class SocketHandler : IDisposable
- {
- private const int MAX_CONNECTIONS = 20;
- private Socket m_serverSocket;
- private ArrayList m_clients = new ArrayList();
-
-
-
-
-
-
-
-
- protected class SocketClient : IDisposable
- {
- private Socket m_socket;
- private StreamWriter m_writer;
-
-
-
-
-
-
-
-
-
- public SocketClient(Socket socket)
- {
- m_socket = socket;
- try
- {
- m_writer = new StreamWriter(new NetworkStream(socket));
- }
- catch
- {
- Dispose();
- throw;
- }
- }
-
-
-
-
-
-
-
-
-
- public void Send(String message)
- {
- m_writer.Write(message);
- m_writer.Flush();
- }
- #region IDisposable Members
-
-
-
-
-
-
-
-
- public void Dispose()
- {
- try
- {
- if (m_writer != null)
- {
- m_writer.Close();
- m_writer = null;
- }
- }
- catch { }
- if (m_socket != null)
- {
- try
- {
- m_socket.Shutdown(SocketShutdown.Both);
- }
- catch { }
- try
- {
- m_socket.Close();
- }
- catch { }
- m_socket = null;
- }
- }
- #endregion
- }
-
-
-
-
-
-
-
-
-
-
- public SocketHandler(int port)
- {
- m_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- m_serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
- m_serverSocket.Listen(5);
- AcceptConnection();
- }
- private void AcceptConnection()
- {
- #if NETSTANDARD1_3
- m_serverSocket.AcceptAsync().ContinueWith(OnConnect, TaskScheduler.Default);
- #else
- m_serverSocket.BeginAccept(new AsyncCallback(OnConnect), null);
- #endif
- }
-
-
-
-
-
-
-
-
-
- public void Send(String message)
- {
- ArrayList localClients = m_clients;
- foreach (SocketClient client in localClients)
- {
- try
- {
- client.Send(message);
- }
- catch (Exception)
- {
-
- client.Dispose();
- RemoveClient(client);
- }
- }
- }
-
-
-
-
- private void AddClient(SocketClient client)
- {
- lock(this)
- {
- ArrayList clientsCopy = (ArrayList)m_clients.Clone();
- clientsCopy.Add(client);
- m_clients = clientsCopy;
- }
- }
-
-
-
-
- private void RemoveClient(SocketClient client)
- {
- lock(this)
- {
- ArrayList clientsCopy = (ArrayList)m_clients.Clone();
- clientsCopy.Remove(client);
- m_clients = clientsCopy;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool HasConnections
- {
- get
- {
- ArrayList localClients = m_clients;
- return (localClients != null && localClients.Count > 0);
- }
- }
-
- #if NETSTANDARD1_3
- private void OnConnect(Task<Socket> acceptTask)
- #else
-
-
-
-
-
-
-
-
-
-
- private void OnConnect(IAsyncResult asyncResult)
- #endif
- {
- try
- {
- #if NETSTANDARD1_3
- Socket socket = acceptTask.GetAwaiter().GetResult();
- #else
-
- Socket socket = m_serverSocket.EndAccept(asyncResult);
- #endif
- LogLog.Debug(declaringType, "Accepting connection from ["+socket.RemoteEndPoint.ToString()+"]");
- SocketClient client = new SocketClient(socket);
- int currentActiveConnectionsCount = m_clients.Count;
- if (currentActiveConnectionsCount < MAX_CONNECTIONS)
- {
- try
- {
- client.Send("TelnetAppender v1.0 (" + (currentActiveConnectionsCount + 1) + " active connections)\r\n\r\n");
- AddClient(client);
- }
- catch
- {
- client.Dispose();
- }
- }
- else
- {
- client.Send("Sorry - Too many connections.\r\n");
- client.Dispose();
- }
- }
- catch
- {
- }
- finally
- {
- if (m_serverSocket != null)
- {
- AcceptConnection();
- }
- }
- }
- #region IDisposable Members
-
-
-
-
-
-
-
-
- public void Dispose()
- {
- ArrayList localClients = m_clients;
- foreach (SocketClient client in localClients)
- {
- client.Dispose();
- }
- m_clients.Clear();
- Socket localSocket = m_serverSocket;
- m_serverSocket = null;
- try
- {
- localSocket.Shutdown(SocketShutdown.Both);
- }
- catch
- {
- }
- try
- {
- localSocket.Close();
- }
- catch
- {
- }
- }
- #endregion
- }
- #endregion
- }
- }
|