jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the 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;
 
using log4net.Layout;
using log4net.Core;
using log4net.Util;
 
namespace log4net.Appender 
{
    /// <summary>
    /// Appender that allows clients to connect via Telnet to receive log messages
    /// </summary>
    /// <remarks>    
    /// <para>
    /// The TelnetAppender accepts socket connections and streams logging messages
    /// back to the client.  
    /// The output is provided in a telnet-friendly way so that a log can be monitored 
    /// over a TCP/IP socket.
    /// This allows simple remote monitoring of application logging.
    /// </para>
    /// <para>
    /// The default <see cref="Port"/> is 23 (the telnet port).
    /// </para>
    /// </remarks>
    /// <author>Keith Long</author>
    /// <author>Nicko Cadell</author>
    public class TelnetAppender : AppenderSkeleton 
    {
        private SocketHandler m_handler;
        private int m_listeningPort = 23;
 
        #region Constructor
 
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <remarks>
        /// <para>
        /// Default constructor
        /// </para>
        /// </remarks>
        public TelnetAppender()
        {
        }
 
        #endregion
 
        #region Private Static Fields
 
        /// <summary>
        /// The fully qualified type of the TelnetAppender class.
        /// </summary>
        /// <remarks>
        /// Used by the internal logger to record the Type of the
        /// log message.
        /// </remarks>
        private readonly static Type declaringType = typeof(TelnetAppender);
 
        #endregion Private Static Fields
 
        /// <summary>
        /// Gets or sets the TCP port number on which this <see cref="TelnetAppender"/> will listen for connections.
        /// </summary>
        /// <value>
        /// An integer value in the range <see cref="IPEndPoint.MinPort" /> to <see cref="IPEndPoint.MaxPort" /> 
        /// indicating the TCP port number on which this <see cref="TelnetAppender"/> will listen for connections.
        /// </value>
        /// <remarks>
        /// <para>
        /// The default value is 23 (the telnet port).
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">The value specified is less than <see cref="IPEndPoint.MinPort" /> 
        /// or greater than <see cref="IPEndPoint.MaxPort" />.</exception>
        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
 
        /// <summary>
        /// Overrides the parent method to close the socket handler
        /// </summary>
        /// <remarks>
        /// <para>
        /// Closes all the outstanding connections.
        /// </para>
        /// </remarks>
        protected override void OnClose()  
        {
            base.OnClose();
 
            if (m_handler != null)
            {
                m_handler.Dispose();
                m_handler = null;
            }
        }
 
        /// <summary>
        /// This appender requires a <see cref="Layout"/> to be set.
        /// </summary>
        /// <value><c>true</c></value>
        /// <remarks>
        /// <para>
        /// This appender requires a <see cref="Layout"/> to be set.
        /// </para>
        /// </remarks>
        protected override bool RequiresLayout
        {
            get { return true; }
        }
 
        /// <summary>
        /// Initialize the appender based on the options set.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This is part of the <see cref="IOptionHandler"/> delayed object
        /// activation scheme. The <see cref="ActivateOptions"/> method must 
        /// be called on this object after the configuration properties have
        /// been set. Until <see cref="ActivateOptions"/> is called this
        /// object is in an undefined state and must not be used. 
        /// </para>
        /// <para>
        /// If any of the configuration properties are modified then 
        /// <see cref="ActivateOptions"/> must be called again.
        /// </para>
        /// <para>
        /// Create the socket handler and wait for connections
        /// </para>
        /// </remarks>
        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;
            }
        }
 
        /// <summary>
        /// Writes the logging event to each connected client.
        /// </summary>
        /// <param name="loggingEvent">The event to log.</param>
        /// <remarks>
        /// <para>
        /// Writes the logging event to each connected client.
        /// </para>
        /// </remarks>
        protected override void Append(LoggingEvent loggingEvent) 
        {
            if (m_handler != null && m_handler.HasConnections)
            {
                m_handler.Send(RenderLoggingEvent(loggingEvent));
            }
        }
 
        #endregion
 
        #region SocketHandler helper class
 
        /// <summary>
        /// Helper class to manage connected clients
        /// </summary>
        /// <remarks>
        /// <para>
        /// The SocketHandler class is used to accept connections from
        /// clients.  It is threaded so that clients can connect/disconnect
        /// asynchronously.
        /// </para>
        /// </remarks>
        protected class SocketHandler : IDisposable
        {            
            private const int MAX_CONNECTIONS = 20;
 
            private Socket m_serverSocket;
            private ArrayList m_clients = new ArrayList();
 
            /// <summary>
            /// Class that represents a client connected to this handler
            /// </summary>
            /// <remarks>
            /// <para>
            /// Class that represents a client connected to this handler
            /// </para>
            /// </remarks>
            protected class SocketClient : IDisposable
            {
                private Socket m_socket;
                private StreamWriter m_writer;
 
                /// <summary>
                /// Create this <see cref="SocketClient"/> for the specified <see cref="Socket"/>
                /// </summary>
                /// <param name="socket">the client's socket</param>
                /// <remarks>
                /// <para>
                /// Opens a stream writer on the socket.
                /// </para>
                /// </remarks>
                public SocketClient(Socket socket)
                {
                    m_socket = socket;
 
                    try
                    {
                        m_writer = new StreamWriter(new NetworkStream(socket));
                    }
                    catch
                    {
                        Dispose();
                        throw;
                    }
                }
 
                /// <summary>
                /// Write a string to the client
                /// </summary>
                /// <param name="message">string to send</param>
                /// <remarks>
                /// <para>
                /// Write a string to the client
                /// </para>
                /// </remarks>
                public void Send(String message)
                {
                    m_writer.Write(message);
                    m_writer.Flush();
                }
 
                #region IDisposable Members
 
                /// <summary>
                /// Cleanup the clients connection
                /// </summary>
                /// <remarks>
                /// <para>
                /// Close the socket connection.
                /// </para>
                /// </remarks>
                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
            }
        
            /// <summary>
            /// Opens a new server port on <paramref ref="port"/>
            /// </summary>
            /// <param name="port">the local port to listen on for connections</param>
            /// <remarks>
            /// <para>
            /// Creates a socket handler on the specified local server port.
            /// </para>
            /// </remarks>
            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);    
                m_serverSocket.BeginAccept(new AsyncCallback(OnConnect), null);
            }
 
            /// <summary>
            /// Sends a string message to each of the connected clients
            /// </summary>
            /// <param name="message">the text to send</param>
            /// <remarks>
            /// <para>
            /// Sends a string message to each of the connected clients
            /// </para>
            /// </remarks>
            public void Send(String message)
            {
                ArrayList localClients = m_clients;
 
                foreach (SocketClient client in localClients)
                {
                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception)
                    {
                        // The client has closed the connection, remove it from our list
                        client.Dispose();
                        RemoveClient(client);
                    }
                }
            }
 
            /// <summary>
            /// Add a client to the internal clients list
            /// </summary>
            /// <param name="client">client to add</param>
            private void AddClient(SocketClient client)
            {
                lock(this)
                {
                    ArrayList clientsCopy = (ArrayList)m_clients.Clone();
                    clientsCopy.Add(client);
                    m_clients = clientsCopy;
                }
            }
 
            /// <summary>
            /// Remove a client from the internal clients list
            /// </summary>
            /// <param name="client">client to remove</param>
            private void RemoveClient(SocketClient client)
            {
                lock(this)
                {
                    ArrayList clientsCopy = (ArrayList)m_clients.Clone();
                    clientsCopy.Remove(client);
                    m_clients = clientsCopy;
                }
            }
 
            /// <summary>
            /// Test if this handler has active connections
            /// </summary>
            /// <value>
            /// <c>true</c> if this handler has active connections
            /// </value>
            /// <remarks>
            /// <para>
            /// This property will be <c>true</c> while this handler has
            /// active connections, that is at least one connection that 
            /// the handler will attempt to send a message to.
            /// </para>
            /// </remarks>
            public bool HasConnections
            {
                get
                {
                    ArrayList localClients = m_clients;
 
                    return (localClients != null && localClients.Count > 0);
                }
            }
            
            /// <summary>
            /// Callback used to accept a connection on the server socket
            /// </summary>
            /// <param name="asyncResult">The result of the asynchronous operation</param>
            /// <remarks>
            /// <para>
            /// On connection adds to the list of connections 
            /// if there are two many open connections you will be disconnected
            /// </para>
            /// </remarks>
            private void OnConnect(IAsyncResult asyncResult)
            {
                try
                {
                    // Block until a client connects
                    Socket socket = m_serverSocket.EndAccept(asyncResult);
 
                    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)
                    {
                        m_serverSocket.BeginAccept(new AsyncCallback(OnConnect), null);
                    }
                }
            }
 
            #region IDisposable Members
 
            /// <summary>
            /// Close all network connections
            /// </summary>
            /// <remarks>
            /// <para>
            /// Make sure we close all network connections
            /// </para>
            /// </remarks>
            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
    }
}