Splunk使用心得

来源:互联网 发布:网络拓扑结构有哪些 编辑:程序博客网 时间:2024/05/16 09:10

最近使用了下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);
        }