最近使用了下Splunk,一個日誌系統,有些心得記錄下。
如果是要用程序記錄日誌,如下操作:
1.配置Splunk打開tcp,udp
2.使用tcp,udp發送數據,當然也可以使用NLog等其它第三方庫來處理
public static void SendByTcp(string strMsg)
{
using (TcpClient cli = new TcpClient("10.86.16.16", 9132))
{
byte[] arrData = System.Text.Encoding.UTF8.GetBytes(GetEntireMessage(strMsg));
using (NetworkStream stream = cli.GetStream())
{
stream.Write(arrData, 0, arrData.Length);
stream.Flush();
}
}
}
public static void SendByUdp(string strMsg)
{
byte[] arrData = System.Text.Encoding.UTF8.GetBytes(GetEntireMessage(strMsg));
UdpClient cli = new UdpClient("10.86.16.16", 9133);
cli.Send(arrData, arrData.Length);
cli.Close();
}
private static string GetEntireMessage(string strMsg)
{
return string.Format("Date={0}, Host={1}, ProcessName={2}, Message={3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Environment.MachineName, Process.GetCurrentProcess().MainModule.ModuleName, strMsg);
}