構成ファイル内で remoteAppender を使用し、ログをリモートで別のコンピューターに送信しています。tcp を使用して情報を送信している間に、remoteAppender が必要なログ文字列ではなく、大量のランダムな記号と文字を送信していることがわかりました。remoteAppender によって送信されているのはログ文字列ではなく、ロガー自体であると思われます。着信ブロードキャストを読み取るために使用した方法は単純で、msdm Web サイトから取得しました。受信したネットワークストリームを文字列に変換し、すべての奇妙な出力を回避する方法を知っている人はいますか?
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☺♥ ♠