/// <summary> /// Selects the given <paramref name="message"/>. /// </summary> /// <param name="message">The <see cref="LogMessage"/> o select</param> public bool SelectLogMessage(LogMessage message) { LogMessageCustom logMessage = message as LogMessageCustom; try { this.SuspendDrawing(); if (logMessage != null) { txtDataNumber.Text = logMessage.Index.ToString(); foreach (Control ctrl in tblLogMessage.Controls) { TextBox txtBox = ctrl as TextBox; if (txtBox == null || !(txtBox.Tag is LogColumn)) { // Ignore not matching textboxes. continue; } txtBox.Text = logMessage.GetValueForColumn(mColumnizer.Columns.IndexOf((LogColumn)txtBox.Tag) + 2).ToString(); txtBox.AdjustHeightToContent(); } } else { txtDataNumber.Text = message != null ? message.Index.ToString() : string.Empty; foreach (Control ctrl in tblLogMessage.Controls) { TextBox txtBox = ctrl as TextBox; if (txtBox == null || !(txtBox.Tag is LogColumn)) { // Ignore not matching textboxes. continue; } txtBox.Text = string.Empty; txtBox.AdjustHeightToContent(); } } pbxCopyNumber.Visible = !string.IsNullOrEmpty(txtDataNumber.Text); } finally { this.ResumeDrawing(); } return(true); }
/// <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; } if (mLastFileOffset > mFileReader.BaseStream.Length) { // the current log file was re-created. Observe from beginning on. mLastFileOffset = 0; } mFileReader.BaseStream.Seek( mLastFileOffset , SeekOrigin.Begin); string currentLine; string messageLines = string.Empty; List <LogMessage> messages = new List <LogMessage>(); while ((currentLine = mFileReader.ReadLine()) != null) { LogMessageCustom cstmLgMsg; try { messageLines += currentLine; cstmLgMsg = new LogMessageCustom( messageLines , mLogNumber + 1 , mColumnizer); } catch (Exception ex) { Logger.Warn(ex.Message); continue; } messageLines = string.Empty; mLogNumber++; messages.Add(cstmLgMsg); } mLastFileOffset = mFileReader.BaseStream.Position; if (mLogHandler != null) { mLogHandler.HandleMessage(messages.ToArray()); } }
/// <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(mEncoding.GetString( receivedBytes , 0 , receivedByteCount)); }while(ns.DataAvailable); LogMessage newLogMsg = new LogMessageCustom( receivedMessage.ToString() , ++mLogNumber , mColumnizer); mLogHandler?.HandleMessage(newLogMsg); } catch (Exception ex) { Logger.Warn(ex.Message); } } } } } catch (Exception ex) { Logger.Warn(ex.Message); } }
/// <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 LogMessageCustom( mEncoding.GetString(receiveBytes) , ++mLogNumber , mColumnizer); if (mLogHandler != null) { mLogHandler.HandleMessage(newLogMsg); } } catch (Exception ex) { Logger.Warn(ex.Message); } } client.BeginReceive(ReceiveUdpMessage, ar.AsyncState); }
/// <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 currentLine; string messageLines = string.Empty; FixedSizedQueue <LogMessage> messages = new FixedSizedQueue <LogMessage>( Properties.Settings.Default.MaxLogMessages); while ((currentLine = mFileReader.ReadLine()) != null) { LogMessageCustom cstmLgMsg; try { messageLines += currentLine; cstmLgMsg = new LogMessageCustom( messageLines , mLogNumber + 1 , mColumnizer); } catch (Exception ex) { mLogHandler.HandleError(LogError.Warn(ex.Message)); continue; } messageLines = string.Empty; mLogNumber++; messages.Enqueue(cstmLgMsg); } mLastFileOffset = mFileReader.BaseStream.Position; mLogHandler?.HandleMessage(messages.ToArray()); }
/// <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; List <LogMessage> messages = new List <LogMessage>(); while ((line = mFileReader.ReadLine()) != null) { LogMessageCustom cstmLgMsg; try { cstmLgMsg = new LogMessageCustom( line , mLogNumber + 1 , mColumnizer); } catch (Exception ex) { Logger.Warn(ex.Message); continue; } mLogNumber++; messages.Add(cstmLgMsg); } mLastFileOffset = mFileReader.BaseStream.Position; if (mLogHandler != null) { mLogHandler.HandleMessage(messages.ToArray()); } }
/// <summary> /// Downloads new messages from the specified <paramref name="url"/>. /// </summary> /// <param name="client">The <see cref="HttpClient"/> to use to download messages.</param> /// <param name="url">The URL to download messages from.</param> /// <returns>The entire count of received lines.</returns> private long Download(HttpClient client, string url) { HttpResponseMessage result = null; try { result = client.GetAsync(url).Result; } catch (Exception ex) { mLogHandler?.HandleError(LogError.Error($"Connection closed unexpected: {(ex.InnerException != null ? ex.InnerException.Message : ex.Message)}")); Shutdown(); return(LastOffset); } if (result == null) { mLogHandler?.HandleError(LogError.Error($"Received unexpected result. Please try to restart the log handler.")); return(LastOffset); } switch (result.StatusCode) { case HttpStatusCode.OK: case HttpStatusCode.PartialContent: { var contentLength = int.Parse(result.Content.Headers.GetValues("Content-Length").FirstOrDefault()); if (contentLength > LastOffset) { using (var downloadClient = new HttpClient()) { downloadClient.DefaultRequestHeaders.Authorization = client.DefaultRequestHeaders.Authorization; if (LastOffset > 0) { downloadClient.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue( contentLength - Math.Max(contentLength - LastOffset, 0), contentLength); } List <LogMessage> receivedLogMessages = new List <LogMessage>(); string downloadedData = mEncoding.GetString(downloadClient.GetByteArrayAsync(url).Result); string messageLines = string.Empty; foreach (string currentLine in downloadedData.Split('\n')) { LogMessageCustom cstmLgMsg; if (string.IsNullOrEmpty(currentLine)) { continue; } try { messageLines += currentLine; cstmLgMsg = new LogMessageCustom( messageLines , ++mLogNumber , mColumnizer); } catch (Exception ex) { mLogHandler.HandleError(LogError.Warn(ex.Message)); continue; } receivedLogMessages.Add(cstmLgMsg); messageLines = string.Empty; } mLogHandler?.HandleMessage(receivedLogMessages.ToArray()); } } return(contentLength); } case HttpStatusCode.Unauthorized: { mLogHandler?.HandleError(LogError.Error("Login failed. Status Code: " + result.StatusCode)); Shutdown(); break; } default: { mLogHandler?.HandleError(LogError.Error("Unexpected Status Code: " + result.StatusCode)); break; } } return(LastOffset); }