public Parse ( ArraySegment |
||
segment | ArraySegment |
|
receivedTime | DateTimeOffset | |
sourceIpAddress | string | |
리턴 | Syslog |
protected override bool TryParse(IpPacket packet, out IEnvelope envelope) { envelope = null; if (!packet.PacketHeader.DestinationIpAddress.Equals(this.ListenEndPoint.Address)) { return(false); } var upacket = packet.ToUdpDatagram(false); var isValid = upacket.UdpDatagramHeader.DestinationPort == this.ListenEndPoint.Port; if (isValid) { Syslog syslog; try { syslog = SyslogParser.Parse( upacket.UdpData, upacket.ReceivedTime, upacket.PacketHeader.SourceIpAddress.ToString()); } catch (Exception) { return(false); } envelope = new SyslogEnvelope { OccurrenceTime = packet.ReceivedTime, ReceivedTime = packet.ReceivedTime, Payload = upacket.UdpData.Array, PayloadInstance = syslog, }; } return(isValid); }
public void TestSyslogCustomParserParsing() { var testprival = "<140>"; var severity = (Severity)Enum.ToObject(typeof(Severity), 140 & 0x7); var facility = (Facility)Enum.ToObject(typeof(Facility), 140 >> 3); var testmessage = "A message with any pri-val"; var testString = testprival + testmessage; var udData = new ArraySegment<byte>(Encoding.ASCII.GetBytes(testString)); var pHeader = new IpPacketHeader( IPAddress.Parse("127.0.0.1"), IPAddress.Parse("127.0.0.1"), false, 4, 0, 0, (ushort)(udData.Array.Length + 20 + 8), 0, 0, 0, 255, 0 ); var udHeader = new UdpDatagramHeader(16, 16, (ushort)udData.Array.Length, 0); UdpDatagram ud = new UdpDatagram() { @UdpDatagramHeader = udHeader, UdpData = udData, PacketHeader = pHeader, ReceivedTime = DateTimeOffset.UtcNow }; var customParser = new Regex(@"(?<WithAny>with\sany)", RegexOptions.ExplicitCapture); var sysparser = new SyslogParser(customParser); var sys = sysparser.Parse(ud); Assert.IsNotNull(sys); Assert.AreEqual(testmessage, sys.Message); Assert.AreEqual(severity, sys.LogSeverity); Assert.AreEqual(facility, sys.LogFacility); Assert.AreEqual("with any", sys.NamedCollectedMatches["WithAny"]); }