Esempio n. 1
0
        /// <summary>
        /// Reads possible new log file entries form the file that is observed.
        /// </summary>
        private void ReadNewLogMessagesFromFile()
        {
            if (mFileReader == null || Equals(mFileReader.BaseStream.Length, mLastFileOffset))
              {
            return;
              }

              mFileReader.BaseStream.Seek(
              mLastFileOffset
            , SeekOrigin.Begin);

              string line;
              string dataToParse = string.Empty;

              List<LogMessage> messages = new List<LogMessage>();

              while ((line = mFileReader.ReadLine()) != null)
              {
            dataToParse += line;

            int log4NetEndTag = dataToParse.IndexOf(
            LOG4NET_LOGMSG_END
              , StringComparison.Ordinal);

            if (log4NetEndTag > 0)
            {
              LogMessage newLogMsg;

              try
              {
            newLogMsg = new LogMessageLog4Net(
                dataToParse
              , ++mLogNumber);
              }
              catch (Exception ex)
              {
            Logger.Warn(ex.Message);
            continue;
              }

              messages.Add(newLogMsg);

              dataToParse = dataToParse.Substring(
              log4NetEndTag
            , dataToParse.Length - (log4NetEndTag + LOG4NET_LOGMSG_END.Length));
            }
              }

              mLastFileOffset = mFileReader.BaseStream.Position;

              if (mLogHandler != null)
              {
            mLogHandler.HandleMessage(messages.ToArray());
              }
        }
Esempio n. 2
0
        /// <summary>
        /// Listen for incomming <see cref="LogMessage"/>s.
        /// </summary>
        /// <param name="newSocket">the <see cref="Socket"/> object that may received new data.</param>
        private void Listening(object newSocket)
        {
            try
              {
            using (Socket socket = (Socket)newSocket)
            {
              using (NetworkStream ns = new NetworkStream(socket, FileAccess.Read, false))
              {
            while (mSocket != null)
            {
              try
              {
                byte[]        receivedBytes     = new byte[1024];
                StringBuilder receivedMessage   = new StringBuilder();

                do
                {
                  int receivedByteCount = ns.Read(
                    receivedBytes
                    , 0
                    , receivedBytes.Length);

                  receivedMessage.Append(Encoding.ASCII.GetString(
                      receivedBytes
                    , 0
                    , receivedByteCount));
                }
                while(ns.DataAvailable);

                LogMessage newLogMsg = new LogMessageLog4Net(
                    receivedMessage.ToString()
                  , ++mLogNumber);

                if (mLogHandler != null)
                {
                  mLogHandler.HandleMessage(newLogMsg);
                }
              }
              catch (Exception ex)
              {
                Logger.Warn(ex.Message);
              }
            }
              }
            }
              }
              catch (Exception ex)
              {
            Logger.Warn(ex.Message);
              }
        }
Esempio n. 3
0
        /// <summary>
        /// Handles the received UDP message event.
        /// </summary>
        /// <param name="ar">The <see cref="IAsyncResult"/> object that contains necessary meta data.</param>
        private void ReceiveUdpMessage(IAsyncResult ar)
        {
            UdpClient client = ((UdpState)ar.AsyncState).Client;

              IPEndPoint wantedIpEndPoint   = ((UdpState)(ar.AsyncState)).EndPoint;
              IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

              Byte[] receiveBytes;

              try
              {
            receiveBytes = client.EndReceive(
            ar
              , ref receivedIpEndPoint);
              }
              catch (ObjectDisposedException)
              {
            // The socket seems to be already closed.
            return;
              }

              bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address))
                       || wantedIpEndPoint.Address.Equals(IPAddress.Any);

              if (isRightHost && receiveBytes != null)
              {
            try
            {
              LogMessage newLogMsg = new LogMessageLog4Net(
              Encoding.ASCII.GetString(receiveBytes)
            , ++mLogNumber);

              if (mLogHandler != null)
              {
            mLogHandler.HandleMessage(newLogMsg);
              }
            }
            catch (Exception ex)
            {
              Logger.Warn(ex.Message);
            }
              }

              client.BeginReceive(ReceiveUdpMessage, ar.AsyncState);
        }