private void OnSyslogMessageReceived(IPAddress sourceAddress, SyslogMessage msg)
 {
     if (SyslogMessageReceived != null)
     {
         SyslogMessageReceived(this, new SyslogEventArgs(sourceAddress, msg));
     }
 }
        public void Send(SyslogMessage msg)
        {
            if (_disposed)
                throw new ObjectDisposedException("SyslogMessage");

            if (_udpClient == null)
                throw new Exception("Cannot send data, connection not established");

            if (msg == null)
                throw new ArgumentNullException("msg", "SyslogMessage paramter null");

            byte[] data = _encoding.GetBytes(msg.ToString());
            _udpClient.Send(data, data.Length);
        }
        public void Send(SyslogMessage msg)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("SyslogMessage");
            }

            if (_udpClient == null)
            {
                throw new Exception("Cannot send data, connection not established");
            }

            if (msg == null)
            {
                throw new ArgumentNullException("msg", "SyslogMessage paramter null");
            }

            byte[] data = _encoding.GetBytes(msg.ToString());
            _udpClient.Send(data, data.Length);
        }
Exemple #4
0
		private void ShowSyslogMessage(IPAddress sourceAddress, SyslogMessage msg)
		{
			if (lstMessages.InvokeRequired)
			{
				ShowSyslogMessageDelegate del = new ShowSyslogMessageDelegate(ShowSyslogMessage);
				this.Invoke(del, new object[] { sourceAddress, msg });
			}
			else
			{
				ListViewItem li = new ListViewItem(
					new string[] { 
									 msg.MessageTime.ToString(),
									 sourceAddress.ToString(),
									 msg.LocalTime.ToString(),
									 msg.Facility.ToString(),
									 msg.Severity.ToString(),
									 msg.Message});

				lstMessages.Items.Add(li);
				lstMessages.EnsureVisible(lstMessages.Items.Count - 1);
			}
		}
        private void ThreadProc()
        {
            bool exiting = false;

            do
            {
                try
                {
                    IPEndPoint remoteHost = _listenPoint;
                    while (true)
                    {
                        Byte[]        receiveBytes = _recUDPClient.Receive(ref remoteHost);
                        string        returnData   = Encoding.ASCII.GetString(receiveBytes);
                        SyslogMessage msg          = SyslogMessage.Parse(remoteHost.Address,
                                                                         returnData);

                        // Fire event
                        OnSyslogMessageReceived(remoteHost.Address, msg);
                    }
                }
                catch (ThreadAbortException)
                {
                    exiting = true;
                    if (ssSwitch.TraceVerbose)
                    {
                        Trace.WriteLine("Message collection thread shutting down",
                                        DbTraceListener.catInfo);
                    }
                }
                catch (Exception ex)
                {
                    if (ssSwitch.TraceError)
                    {
                        Trace.WriteLine(String.Format("Error reciving syslog message: {0}",
                                                      ex.Message), DbTraceListener.catError);
                    }
                }
            } while (!exiting);
        }
 public SyslogEventArgs(IPAddress sourceAddress, SyslogMessage msg)
 {
     _sourceAddress = sourceAddress;
     _msg = msg;
 }
		public static SyslogMessage Parse(IPAddress hostIP, string syslogString)
		{
			FacilityCode fc;
			SeverityCode sc;
			DateTime recdTime;
			string msg;

			// Strip out all non-printable characters - replace with spaces.
			string strippedString = _msgInvalidChars.Replace(syslogString, " ");

			try
			{
				// Try parse PRI, date and message
				// Note - hostname not matched as very few devices conform to RFC
				Match m = _fullFormat.Match(strippedString);
				if ((m != null) && m.Success && (m.Groups.Count == 4))
				{
					ParseCode(m.Groups[1].ToString(), out fc, out sc);

					// Try to parse both date formats
					try
					{
						recdTime = DateTime.ParseExact(m.Groups[2].ToString(), _dateFormat1, _standardCulture);
					}
					catch (FormatException)
					{
						recdTime = DateTime.ParseExact(m.Groups[2].ToString(), _dateFormat2, _standardCulture);
					}

					msg = m.Groups[3].ToString();
					return new SyslogMessage(hostIP.ToString(), msg, fc, sc, recdTime);
				} 
				else 
				{
					throw new Exception("Cannot parse message");
				}
			}
			catch (Exception)
			{
				// Try and parse PRI only
				try
				{
					Match m = _priFormat.Match(strippedString);
					if ((m != null) && m.Success && (m.Groups.Count == 3))
					{
						ParseCode(m.Groups[1].ToString(), out fc, out sc);
						msg = m.Groups[2].ToString();
						SyslogMessage sm = new SyslogMessage(hostIP.ToString(), msg);
						sm._facility = fc;
						sm._severity = sc;
						return sm;
					}
					else 
					{
						throw new Exception("Cannot parse message");
					}
				}

				catch (Exception)
				{
					// Cannot decode at all
					return new SyslogMessage(hostIP.ToString(), strippedString);
				}
			}
		}
 public SyslogEventArgs(IPAddress sourceAddress, SyslogMessage msg)
 {
     _sourceAddress = sourceAddress;
     _msg           = msg;
 }
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			if (ValidInput()) 
			{
				// Wait cursor
				using (new WaitCursor())

				try
				{
					// Lookup server IP Address
					IPHostEntry IPList = Dns.GetHostEntry(txtServer.Text);
					if ((IPList == null) || (IPList.AddressList.Length == 0))
						throw new Exception("Unable to resolve address for host " + 
							txtServer.Text);

					IPAddress destAddress = IPList.AddressList[0];

					// Get our host name
					string hostName = Dns.GetHostName();

					// Create message
					SyslogMessage msg = new SyslogMessage(hostName,
						txtMessage.Text,
						(SyslogMessage.FacilityCode) comFacility.SelectedItem,
						(SyslogMessage.SeverityCode) comSeverity.SelectedItem,
						DateTime.Now);

					// Send message
					using (SyslogClient client = new SyslogUdpClient())
					{
                        client.Connect(destAddress, Convert.ToInt32(txtPort.Text));
						client.Send(msg);
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(this, "Unable to send message:\n" + ex.Message);
				}
			}
		}
        public static SyslogMessage Parse(IPAddress hostIP, string syslogString)
        {
            FacilityCode fc;
            SeverityCode sc;
            DateTime     recdTime;
            string       msg;

            // Strip out all non-printable characters - replace with spaces.
            string strippedString = _msgInvalidChars.Replace(syslogString, " ");

            try
            {
                // Try parse PRI, date and message
                // Note - hostname not matched as very few devices conform to RFC
                Match m = _fullFormat.Match(strippedString);
                if ((m != null) && m.Success && (m.Groups.Count == 4))
                {
                    ParseCode(m.Groups[1].ToString(), out fc, out sc);

                    // Try to parse both date formats
                    try
                    {
                        recdTime = DateTime.ParseExact(m.Groups[2].ToString(), _dateFormat1, _standardCulture);
                    }
                    catch (FormatException)
                    {
                        recdTime = DateTime.ParseExact(m.Groups[2].ToString(), _dateFormat2, _standardCulture);
                    }

                    msg = m.Groups[3].ToString();
                    return(new SyslogMessage(hostIP.ToString(), msg, fc, sc, recdTime));
                }
                else
                {
                    throw new Exception("Cannot parse message");
                }
            }
            catch (Exception)
            {
                // Try and parse PRI only
                try
                {
                    Match m = _priFormat.Match(strippedString);
                    if ((m != null) && m.Success && (m.Groups.Count == 3))
                    {
                        ParseCode(m.Groups[1].ToString(), out fc, out sc);
                        msg = m.Groups[2].ToString();
                        SyslogMessage sm = new SyslogMessage(hostIP.ToString(), msg);
                        sm._facility = fc;
                        sm._severity = sc;
                        return(sm);
                    }
                    else
                    {
                        throw new Exception("Cannot parse message");
                    }
                }

                catch (Exception)
                {
                    // Cannot decode at all
                    return(new SyslogMessage(hostIP.ToString(), strippedString));
                }
            }
        }
		private void OnSyslogMessageReceived(IPAddress sourceAddress, SyslogMessage msg)
		{
			if (SyslogMessageReceived != null)
				SyslogMessageReceived(this, new SyslogEventArgs(sourceAddress, msg));
		}