This commit introduces the AppDiagnostic project structure, including core components like `MainForm`, logging utilities (`LogAdapter`, `LogFilter`), and configuration files. It integrates with `log4net` for logging and uses `SharedComponents` for shared utilities.
267 lines
9.5 KiB
C#
267 lines
9.5 KiB
C#
using log4net.Config;
|
|
using log4net;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using log4net.Appender;
|
|
using SharedComponents;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
using System.IO; // Pre File.WriteAllLines a prácu so súbormi
|
|
using System.Linq;
|
|
|
|
namespace AppDiagnostic
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
private bool stopUpdate = false; // Premenná na zastavenie aktualizácie
|
|
private int lastDisplayedLogIndex = 0;
|
|
private bool autoScrollEnabled = true; // Automatický posun, predvolene povolený
|
|
private MainForm appDiagnosticForm;
|
|
private Timer refreshTimer;
|
|
private LogAdapter _logAdapter; // Pre LogAdapter
|
|
private LogFilter _logFilter; // LogFilter objekt pre filtráciu
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// Inicializujeme LogAdapter a priradíme ho k memoListView
|
|
_logAdapter = new LogAdapter(memoListView);
|
|
|
|
RefreshLogView();
|
|
|
|
// Pripojenie na udalosť pri pridaní nového logu
|
|
// nakoľko sa memo refreshuje cyklicky, tak refresh na udalosť nepotrebujem
|
|
//LiveLogCache.Instance.LogAdded += OnLogAdded;
|
|
|
|
// Vytvoríme LogFilter objekt
|
|
//_logFilter = new LogFilter(memoListView);
|
|
}
|
|
|
|
private void OnLogAdded(string log)
|
|
{
|
|
try
|
|
{
|
|
RefreshLogView();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Error in OnLogAdded: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private void RefreshLogView()
|
|
{
|
|
// Skontrolujte, či voláme z iného vlákna
|
|
if (InvokeRequired)
|
|
{
|
|
Invoke((Action)RefreshLogView);
|
|
return;
|
|
}
|
|
|
|
memoListView.BeginUpdate(); // Zabraňuje vizuálnym aktualizáciám počas vykresľovania
|
|
|
|
try
|
|
{
|
|
// Skontrolujte, či je užívateľ na poslednom viditeľnom zázname
|
|
if (memoListView.Items.Count > 0)
|
|
{
|
|
var lastVisibleIndex = memoListView.TopItem.Index + memoListView.ClientRectangle.Height / memoListView.Items[0].Bounds.Height;
|
|
autoScrollEnabled = lastVisibleIndex >= memoListView.Items.Count - 1;
|
|
}
|
|
|
|
// Získajte nové logy od posledného indexu
|
|
var logs = LiveLogCache.Instance.GetLogs(lastDisplayedLogIndex, LiveLogCache.Instance.Logs.Count - lastDisplayedLogIndex);
|
|
|
|
// Pridajte len nové logy
|
|
foreach (var log in logs)
|
|
{
|
|
memoListView.Items.Add(new ListViewItem(log));
|
|
}
|
|
|
|
// Aktualizujte posledný zobrazený index
|
|
lastDisplayedLogIndex = LiveLogCache.Instance.Logs.Count;
|
|
|
|
// Ak je autoScrollEnabled, posuňte sa na posledný záznam
|
|
if (autoScrollEnabled && memoListView.Items.Count > 0)
|
|
{
|
|
memoListView.EnsureVisible(memoListView.Items.Count - 1);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
memoListView.EndUpdate(); // Umožní vizuálne aktualizácie
|
|
}
|
|
}
|
|
|
|
// Táto metóda sa spustí pri scrollovaní v memoListView (tzn. ak sa používateľ dostane na spodok listu)
|
|
private void memoListView_Scroll(object sender, EventArgs e)
|
|
{
|
|
// Skontrolujte, či používateľ dosiahol spodok ListView
|
|
if (memoListView.Items.Count > 0 &&
|
|
memoListView.Items[memoListView.Items.Count - 1].Bounds.Bottom <= memoListView.ClientSize.Height)
|
|
{
|
|
// Ak áno, načítame ďalšie logy
|
|
RefreshLogView();
|
|
}
|
|
}
|
|
|
|
private void button6_Click(object sender, EventArgs e)
|
|
{
|
|
//LiveLogCache.Instance.AddLog("Nový log z hlavnej aplikácie");
|
|
|
|
LiveLogCache.Instance.ClearLogs();
|
|
ClearListView();
|
|
}
|
|
|
|
private void runButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (stopUpdate) // Ak je časovač zastavený, spustíme ho
|
|
{
|
|
stopUpdate = false; // Zastav aktualizáciu
|
|
runButton.Enabled = false; // Zakážeme tlačidlo Start počas behu
|
|
stopButton.Enabled = true; // Povolenie tlačidla Stop
|
|
refreshTimer.Start();
|
|
}
|
|
}
|
|
|
|
private void stopButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (!stopUpdate) // Ak časovač beží, môžeme ho zastaviť
|
|
{
|
|
stopUpdate = true; // Spusti aktualizáciu
|
|
runButton.Enabled = true; // Povolenie tlačidla Start
|
|
stopButton.Enabled = false; // Zakážeme tlačidlo Stop
|
|
refreshTimer.Stop();
|
|
}
|
|
}
|
|
|
|
private void ClearListView()
|
|
{
|
|
if (InvokeRequired)
|
|
{
|
|
Invoke(new Action(ClearListView));
|
|
return;
|
|
}
|
|
|
|
memoListView.Items.Clear();
|
|
}
|
|
|
|
private void refreshMemoButton_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void button7_Click(object sender, EventArgs e)
|
|
{
|
|
SaveLogsToFile();
|
|
//LiveLogCache.Instance.AddLog("Toto je nový log.");
|
|
}
|
|
|
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
|
{
|
|
// Odpojenie udalosti, keď sa okno zatvára, inak môže po opätovnom spustení okna a pridaní nového itemu do listu zhhodiť program
|
|
// nakoľko sa memo refreshuje cyklicky, tak refresh na udalosť nepotrebujem
|
|
//LiveLogCache.Instance.LogAdded -= OnLogAdded;
|
|
|
|
_logAdapter = null;
|
|
|
|
base.OnFormClosing(e);
|
|
}
|
|
|
|
private void filterButton_Click_1(object sender, EventArgs e)
|
|
{
|
|
// Aplikujeme filter na základe textu z filterTextBox
|
|
string filterText = filterTextBox.Text;
|
|
//_logFilter.ApplyFilter(filterText); // Aplikovanie filtra
|
|
_logAdapter.ApplyFilter(filterText);
|
|
}
|
|
private void SubForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// Vaša logika pri uzatváraní formy
|
|
// this.Hide(); // Podforma sa len skryje
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Ošetrenie výnimky
|
|
MessageBox.Show("Chyba pri zatváraní pod-aplikácie: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private void SaveLogsToFile()
|
|
{
|
|
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
|
{
|
|
saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
|
|
saveFileDialog.Title = "Save TBF live logs";
|
|
saveFileDialog.DefaultExt = "txt";
|
|
saveFileDialog.FileName = "TBFLiveLogs.txt";
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
// Načítanie všetkých logov z LiveLogCache
|
|
var logs = LiveLogCache.Instance.GetLogs(0, LiveLogCache.Instance.GetCount());
|
|
|
|
// Uloženie logov do vybraného súboru
|
|
File.WriteAllLines(saveFileDialog.FileName, logs);
|
|
|
|
MessageBox.Show("Logs saved successfully.", "Save TBF live logs", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"An error occurred while saving TBF live logs: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void button2_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
//this.Hide(); // Podforma sa len skryje
|
|
}
|
|
|
|
private void MemoListView_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
if (memoListView.SelectedItems.Count > 0)
|
|
{
|
|
var selectedItem = memoListView.SelectedItems[0];
|
|
int selectedIndex = memoListView.Items.IndexOf(selectedItem);
|
|
|
|
if (selectedIndex >= 0)
|
|
{
|
|
// Zrušenie filtra
|
|
_logAdapter._filterText = string.Empty;
|
|
|
|
// Načítanie logov od indexu vybraného riadku
|
|
var logs = LiveLogCache.Instance.GetLogs(selectedIndex, LiveLogCache.Instance.GetCount() - selectedIndex);
|
|
|
|
// Obnovenie ListView s novými údajmi
|
|
memoListView.BeginUpdate();
|
|
memoListView.Items.Clear();
|
|
foreach (var log in logs)
|
|
{
|
|
var item = new ListViewItem(log)
|
|
{
|
|
BackColor = memoListView.Items.Count % 2 == 0 ? Color.White : Color.FromArgb(240, 240, 240)
|
|
};
|
|
memoListView.Items.Add(item);
|
|
}
|
|
memoListView.EndUpdate();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|