예제 #1
0
        public void Write(LogMessage logMessage)
        {
            if (logMessage != null)
            {
                UDPLogConfig config = logMessage.LogActor.Config as UDPLogConfig;

                UdpClient udpClient = null;

                if (!udpList.ContainsKey(config.RemoteUdpAddress))
                {
                    udpClient = new UdpClient();
                    udpList.Add(config.RemoteUdpAddress, udpClient);
                }
                else
                {
                    udpClient = udpList[config.RemoteUdpAddress];
                }

                IPEndPoint remoteEndPoint = new IPEndPoint(config.RemoteUdpAddress, config.RemoteUdpPort);
                byte[]     sendBytes      = Encoding.Unicode.GetBytes(logMessage.Message);
                udpClient.Send(sendBytes, sendBytes.Length, remoteEndPoint);
            }
        }
        private void ReadUDPLogConfig()
        {
            System.Xml.XmlNode udp = this.xmlNode.SelectSingleNode("//UDP");
            if (udp != null)
            {
                System.Xml.XmlNodeList configs = udp.SelectNodes("Config");
                foreach (System.Xml.XmlNode config in configs)
                {
                    //id="capture" category="" level="DEBUG" module="capture" protocolType="IPv4" remoteUDPAddress="127.0.0.1" remoteUDPPort="1024"
                    System.Xml.XmlAttribute id = config.Attributes["id"];
                    System.Xml.XmlAttribute category = config.Attributes["category"];
                    System.Xml.XmlAttribute level = config.Attributes["level"];
                    System.Xml.XmlAttribute module = config.Attributes["module"];
                    System.Xml.XmlAttribute protocolType = config.Attributes["protocolType"];
                    System.Xml.XmlAttribute remoteUDPAddress = config.Attributes["remoteUDPAddress"];
                    System.Xml.XmlAttribute remoteUDPPort = config.Attributes["remoteUDPPort"];
                    if (id == null || protocolType == null || remoteUDPAddress == null || remoteUDPPort == null)
                    {
                        throw new StackException("config error: dns config should have id, protocolType, remoteUDPAddress and remoteUDPPort.");
                    }

                    // create a config instance
                    UDPLogConfig udpLogConfig = new UDPLogConfig();
                    udpLogConfig.Category = category.Value;
                    udpLogConfig.Level = (LogLevel)Enum.Parse(typeof(LogLevel), level.Value);
                    udpLogConfig.Module = module.Value;
                    udpLogConfig.ProtoType = (ProtocolType)Enum.Parse(typeof(ProtocolType), protocolType.Value);
                    foreach (IPAddress address in System.Net.Dns.GetHostAddresses(remoteUDPAddress.Value))
                    {
                        if (
                            (udpLogConfig.ProtoType == ProtocolType.IPv4 && address.AddressFamily == AddressFamily.InterNetwork)
                            ||
                            (udpLogConfig.ProtoType == ProtocolType.IPv6 && address.AddressFamily == AddressFamily.InterNetworkV6)
                            )
                        {
                            udpLogConfig.RemoteUdpAddress = address;
                            break;
                        }
                    }
                    udpLogConfig.RemoteUdpPort = Convert.ToInt32(remoteUDPPort.Value, CultureInfo.InvariantCulture);

                    if (this.logConfigs.ContainsKey(id.Value))
                    {
                        throw new StackException("the id of config must be unique.");
                    }
                    this.logConfigs.Add(id.Value, udpLogConfig);
                }
            }
        }