所以我的好老板跟我提出了一个问题,因为我知道我的方式围绕C#(不是很好但有点)并且询问是否有可能构建一个简单的Syslog服务器而不是出去买一个。所以当然我接受了挑战并前往城镇。我已经开发了一个Alpha(其中应用程序将所有内容直接输入到数据库中),但他问我是否可以执行客户端与侦听器方法(PIA但合理)。现在我相信我已经正确构造了TCPClient但是我被困在TCPlistener部分并且想要一些帮助,如果有人能指出我正确的方向。下面是我到目前为止编写的3个程序(alpha,客户端,以及监听器的一部分)
α
EventLog alog = new EventLog();
alog.Log = "Application";
alog.MachineName = ".";
DateTime fifteenMinsAgo = DateTime.Now.AddMinutes(-15);
foreach (EventLogEntry entry in alog.Entries)
{
if (entry.TimeGenerated > fifteenMinsAgo)
{
SqlConnection sql = new SqlConnection("Server=x.x.x.x;Database=yy;User ID=abcd;password=efg");
sql.Open();
SqlCommand cmd = new SqlCommand(@"insert into ApplicationLog(Level,DateTime,Source,EventID,TaskCategory,ServerName,Message) values (@Level,@DateTime,@Source,@EventID,@TaskCategory,@ServerName,@Message)", sql);
cmd.Parameters.AddWithValue("@Level", entry.EntryType);
cmd.Parameters.AddWithValue("@DateTime", entry.TimeGenerated);
cmd.Parameters.AddWithValue("@Source", entry.Source);
cmd.Parameters.AddWithValue("@EventID", entry.InstanceId);
cmd.Parameters.AddWithValue("@TaskCategory", entry.Category);
cmd.Parameters.AddWithValue("@ServerName", entry.MachineName);
cmd.Parameters.AddWithValue("@Message", entry.Message);
cmd.ExecuteNonQuery();
sql.Close();
}
}
的TcpClient:
EventLog alog = new EventLog();
alog.Log = "Application";
alog.MachineName = ".";
DateTime fifteenMinsAgo = DateTime.Now.AddMinutes(-15);
foreach (EventLogEntry entry in alog.Entries)
{
if (entry.TimeGenerated > fifteenMinsAgo)
{
TcpClient client = new TcpClient();
IPEndPoint serverendpoint = new IPEndPoint(IPAddress.Parse("x.x.x.x"), yyyy);
client.Connect(serverendpoint);
NetworkStream clientstream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(entry.ToString());
clientstream.Write(buffer, 0, buffer.Length);
clientstream.Flush();
}
}
的TCPListener:
TcpListener tcpServerListener = new TcpListener(IPAddress.Parse("127.0.0.1"), yyyy);
tcpServerListener.Start();
Console.WriteLine("Server Started");
Socket serverSocket = tcpServerListener.AcceptSocket();
if (serverSocket.Connected)
{
NetworkStream serverSockStream = new NetworkStream(serverSocket);
serverSockStream.ReadByte();
}