private void UpdateText(SyslogMessage sm) { grdMessages.Rows.Add(new Object[] {sm.Timestamp.ToString(),sm.Hostname, sm.Severity.Value.ToString(), sm.Facility.Value.ToString(), sm.Message}); }
/// <summary> /// This internal method processes an async receive as set up by SetupReceive() /// </summary> private void DoReceiveData(IAsyncResult r) { Socket sock = (Socket)r.AsyncState; //int count = sock.EndReceive(r, out errorCode); EndPoint ep = new IPEndPoint(IPAddress.Any, 0); int count = 0; try { count = sock.EndReceiveFrom(r, ref ep); } catch (SocketException) { //ignore buffer overruns; .NET handles them. } catch (ObjectDisposedException) { //if the socket is disposed, we're shutting down, so return return; } string packet = System.Text.Encoding.ASCII.GetString(receiveBuffer, 0, count); Match m = msgRegex.Match(packet); //ignore invalid messages if (m != null && !string.IsNullOrEmpty(packet)) { //parse PRI section into priority int pri; int? priority = int.TryParse(m.Groups["PRI"].Value, out pri) ? new int?(pri) : null; //parse the HEADER section - contains TIMESTAMP and HOSTNAME string hostname = null; Nullable<DateTime> timestamp = null; if (!string.IsNullOrEmpty(m.Groups["HDR"].Value)) { if (!string.IsNullOrEmpty(m.Groups["TIMESTAMP"].Value)) { try { timestamp = new DateTime( DateTime.Now.Year, MonthNumber(m.Groups["MMM"].Value), int.Parse(m.Groups["DD"].Value), int.Parse(m.Groups["HH"].Value), int.Parse(m.Groups["MM"].Value), int.Parse(m.Groups["SS"].Value) ); } catch (ArgumentException) { //ignore invalid timestamps } } if (!string.IsNullOrEmpty(m.Groups["HOSTNAME"].Value)) { hostname = m.Groups["HOSTNAME"].Value; } } if (!timestamp.HasValue) { //add timestamp as per RFC3164 timestamp = DateTime.Now; } if (string.IsNullOrEmpty(hostname)) { IPEndPoint ipe = (IPEndPoint)ep; try { IPHostEntry he = Dns.GetHostEntry(ipe.Address); if (he != null && !string.IsNullOrEmpty(he.HostName)) hostname = he.HostName; else hostname = ep.ToString(); } catch { hostname = ep.ToString(); } } string message = m.Groups["MSG"].Value; SyslogMessage sm = new SyslogMessage(priority, timestamp.Value, hostname, message); if (MessageReceived != null) { MessageReceived(new MessageReceivedEventArgs(sm)); } } //after we're done processing, ready the socket for another receive. SetupReceive(); }
/// <summary> /// Posts a new message to the buffer, for committing to the database when the internal timer elapses. /// </summary> /// <param name="sm">The syslog message to be submitted.</param> public void PushMessage(SyslogMessage sm) { bufferLock.WaitOne(); buffer.Add(sm); bufferLock.ReleaseMutex(); }
/// <summary> /// Creates a new instance of the MessageReceivedEventArgs class. /// </summary> public MessageReceivedEventArgs(SyslogMessage sm) : base() { this.syslogMessage = sm; }