1

我在配置文件中使用 remoteAppender 并将日志远程发送到另一台计算机。在使用 tcp 发送信息时,我发现 remoteAppender 也在发送大量随机符号和字符,而不是我想要的日志字符串。我怀疑 remoteAppender 发送的不是日志字符串,而是记录器本身。我在传入广播中读取的方法很简单,取自 msdm 网站。有谁知道将接收到的网络流转换回字符串并避免所有奇怪输出的方法?

public void StartListener()
    {
        try
        {
            // Set the TcpListener on port 13000.
            Int32 port = 32100;

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(IPAddress.Any, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            // Enter the listening loop. 
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests. 
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client. 
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
                    Console.WriteLine("Received: {0}", data);

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine("Sent: {0}", data);
                }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
        catch (Exception e)
        {
            Console.WriteLine("Connection was terminated");
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }


        Console.WriteLine("\nHit enter to continue...");
        Console.Read();

    }

这是我通过 remoteAppender 发送日志时得到的输出:

Waiting for a connection... Connected!
Received: .NET☺     /♥  ♦ ☺☺$   tcp://149.63.90.84:32100/LoggingSink♠ ☺☺↑   appl
ication/octet-stream
Sent: .NET☺     /♥  ♦ ☺☺$   TCP://149.63.90.84:32100/LOGGINGSINK♠ ☺☺↑   APPLICAT
ION/OCTET-STREAM
Received:  ☺   ????☺       §¶   ↕       LogEvents↕?☺log4net.Appender.RemotingApp
ender+IRemoteLoggingSink, log4net, Version=1.2.11.0, Culture=neutral, PublicKeyT
oken=669e0ddf0bb1aa2a►☺   ☺     ☻   ♀♥   Klog4net, Version=1.2.11.0, Culture=neu
tral, PublicKeyToken=669e0ddf0bb1
Sent:  ☺   ????☺       §¶   ↕   LOGEVENTS↕?☺LOG4NET.APPENDER.REMOTINGAPPENDER+IR
EMOTELOGGINGSINK, LOG4NET, VERSION=1.2.11.0, CULTURE=NEUTRAL, PUBLICKEYTOKEN=669
E0DDF0BB1AA2A►☺   ☺     ☻   ♀♥   KLOG4NET, VERSION=1.2.11.0, CULTURE=NEUTRAL, PU
BLICKEYTOKEN=669E0DDF0BB1
Received: aa2a☻    ☺   ☺   ♦↓log4net.Core.LoggingEvent♥         ♦   ♣♦   ↓log4ne
t.Core.LoggingEvent♂
LoggerName♣LevelMessage
ThreadName      TimeStamp♀LocationInfUserName☼ExceptionString
!log4net.Util.Propertity☺♦☺☺ ☻☻☺♦☺☻↕log4net.Core.Level♥
Sent: AA2A☻    ☺   ☺   ♦↓LOG4NET.CORE.LOGGINGEVENT♥     ♦   ♣♦   ↓LOG4NET.CORE.L
OGGINGEVENT♂
LOGGERNAME♣LEVELMESSAGE
THREADNAME      TIMESTAMP♀LOCATIONINFUSERNAME☼EXCEPTIONSTRING
!LOG4NET.UTIL.PROPERTITY☺♦☺☺ ☻☻☺♦☺☻↕LOG4NET.CORE.LEVEL♥
Received: iesDictionary♥   ♥   ♠♣   ►Project2.Program   ♠   ♠   ♥Hi0   ♦6384?:??
Uf?

♠
   ♠♂   ‼Project2.vshost.exe
♣♠   ↕log4net.Core.Level♥   ♀m_levelValue♂m_levelName↕m_levelDisplayName ♥   p◄☺
 ♠♀   ♣ERROR    ♀   ♣
   !log4net.Util.PropertiesDictionary☺
Sent: IESDICTIONARY♥   ♥   ♠♣   ►PROJECT2.PROGRAM       ♠   ♠   ♥HI0   ♦6384?:??
UF?

♠
   ♠♂   ‼PROJECT2.VSHOST.EXE
♣♠   ↕LOG4NET.CORE.LEVEL♥   ♀M_LEVELVALUE♂M_LEVELNAME↕M_LEVELDISPLAYNAME ♥   P◄☺
 ♠♀   ♣ERROR    ♀   ♣
   !LOG4NET.UTIL.PROPERTIESDICTIONARY☺
   ♂KOLLIK-D1SD♂net_x003A_HostName☺♥   ♠
   ♂KOLLIK-D1SD♂X003A_HOSTNAME☺♥   ♠
4

1 回答 1

0

RemotingAppender 使用 .NET 远程处理将日志事件代理到实现 log4net.Appender.RemotingAppender+IRemoteLoggingSink 接口的端点。看起来您正试图通过 TCP 简单地读取呈现的日志消息,它正在“工作”,但正如您所指出的,您的数据流不包含呈现的日志字符串,而是包含远程二进制日志事件。

改用 TelnetAppender 或 UDPAppender;这些附加程序在通过网络发送之前呈现日志消息,因此您将获得您期望的字符串。

请参阅http://logging.apache.org/log4net/release/sdk/log4net.Appender.TelnetAppender.htmlhttp://maddemcode.com/net/secrets-of-log4net-telnetappender/

于 2013-08-30T12:34:57.443 回答