389 lines
11 KiB
C#
389 lines
11 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace SteamWare
|
|
{
|
|
/// <summary>
|
|
/// classe gesione log files applicazioni
|
|
/// </summary>
|
|
public class logger
|
|
{
|
|
#region dichiarazione variabili
|
|
|
|
#region altri dati
|
|
|
|
/// <summary>
|
|
/// directory base x logs
|
|
/// </summary>
|
|
protected string _logBaseDir;
|
|
/// <summary>
|
|
/// nome del file corrente
|
|
/// </summary>
|
|
protected string _logfileName;
|
|
/// <summary>
|
|
/// max mb di log da accumulare
|
|
/// </summary>
|
|
protected int _logMaxMb;
|
|
/// <summary>
|
|
/// controlla se si debba mantenere sotto controllo la dimensioen della cartella logs
|
|
/// </summary>
|
|
protected bool _doShrinkFolder;
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region metodi esposti
|
|
|
|
/// <summary>
|
|
/// singleton del logger
|
|
/// </summary>
|
|
public static logger lg = new logger();
|
|
|
|
/// <summary>
|
|
/// avvio del logger nella dir desiderata
|
|
/// </summary>
|
|
public logger()
|
|
{
|
|
try
|
|
{
|
|
_logBaseDir = SteamwareStrings.getFilePath(memLayer.ML.confReadString("_logDir"));
|
|
}
|
|
catch
|
|
{
|
|
_logBaseDir = memLayer.ML.confReadString("logLocalDir");
|
|
}
|
|
_logMaxMb = 100; // di default 100 mb...
|
|
try
|
|
{
|
|
_doShrinkFolder = memLayer.ML.confReadBool("doShrinkFolder");
|
|
}
|
|
catch
|
|
{
|
|
_doShrinkFolder = true;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// livello di log applicazione (da web.config, chiave '_logLevel')
|
|
/// </summary>
|
|
public int logLevel
|
|
{
|
|
get
|
|
{
|
|
int answ = -1;
|
|
try
|
|
{
|
|
answ = memLayer.ML.confReadInt("_logLevel");
|
|
}
|
|
catch
|
|
{
|
|
scriviLog("non ho trovato chiave di registro x logLevel...", tipoLog.ERROR);
|
|
}
|
|
return answ;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// avvio del logger nella dir desiderata
|
|
/// </summary>
|
|
public logger(string _baseDir)
|
|
{
|
|
_logBaseDir = _baseDir;
|
|
_logMaxMb = 40; // di default 40 mb...
|
|
}
|
|
/// <summary>
|
|
/// avvio del logger nella dir desiderata con il max di dati indicato
|
|
/// </summary>
|
|
public logger(string _baseDir, int _logMaxMB)
|
|
{
|
|
_logBaseDir = _baseDir;
|
|
_logMaxMb = _logMaxMB;
|
|
}
|
|
/// <summary>
|
|
/// resetta il logfile odierno
|
|
/// </summary>
|
|
public void resetLogFile()
|
|
{
|
|
newLogfileName();
|
|
FileInfo fi = new FileInfo(_logfileName);
|
|
fi.Delete();
|
|
scriviLog("ResetFile", tipoLog.STARTUP);
|
|
}
|
|
/// <summary>
|
|
/// scrive sul file log di default il valore della variabile string passata su una riga... (tab delim?!?)
|
|
/// </summary>
|
|
/// <param name="_testoPre">testo iniziale del log</param>
|
|
/// <returns></returns>
|
|
public bool scriviLog(string _testoPre)
|
|
{
|
|
// attenzione: rimpiazzo eventuali "</br>" con newline...
|
|
string _testo = string.Format("{0:H:mm:ss ffff} \t{1}", DateTime.Now, _testoPre.Replace("<br>", Environment.NewLine).Replace("<br/>", Environment.NewLine).Replace("<br />", Environment.NewLine));
|
|
|
|
if (_doShrinkFolder)
|
|
{
|
|
// verifica dim directory ed eventualmente cancella... - opzionale
|
|
shrinkDir();
|
|
}
|
|
// (ri)genera il nome del file di log...
|
|
newLogfileName();
|
|
// aggiunge in append...
|
|
StreamWriter sw = new StreamWriter(_logfileName, true);
|
|
bool fatto;
|
|
try
|
|
{
|
|
sw.WriteLine(_testo);
|
|
sw.Flush();
|
|
sw.Close();
|
|
fatto = true;
|
|
}
|
|
catch
|
|
{
|
|
fatto = false;
|
|
}
|
|
return fatto;
|
|
}
|
|
/// <summary>
|
|
/// scrive un messaggio di log con etichetta pre
|
|
/// </summary>
|
|
/// <param name="testoLog">testo messaggio</param>
|
|
/// <param name="tipo">tipo di log da registrare (etichetta [...])</param>
|
|
/// <returns></returns>
|
|
public bool scriviLog(string testoLog, tipoLog tipo)
|
|
{
|
|
bool answ = false;
|
|
answ = scriviLog(string.Format("[{0}] - {1}", tipo, testoLog));
|
|
// se è un tipo EXCEPTION allora scrivo anche log diagnostico
|
|
if (tipo == tipoLog.EXCEPTION)
|
|
{
|
|
try
|
|
{
|
|
// se abilitato output diagnostica...
|
|
if (memLayer.ML.confReadBool("enableDumpDiag"))
|
|
{
|
|
string[] pingTagets = SteamWare.StringSplitter.CSplitter.Split(memLayer.ML.confReadString("pingTargets"), "#", true, -1, SteamWare.StringSplitter.ComparisonMethod.Text);
|
|
string[] wwwTagets = SteamWare.StringSplitter.CSplitter.Split(memLayer.ML.confReadString("wwwTargets"), "#", true, -1, SteamWare.StringSplitter.ComparisonMethod.Text);
|
|
// chiamo scrittura diagnostica
|
|
scriviDiagnostica("DUMP diagnostico causa Eccezione", pingTagets, wwwTagets);
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// scrive su log un dump di diagnostica
|
|
/// </summary>
|
|
/// <param name="testoLog">Causale diagnostica</param>
|
|
/// <param name="pingTargets">target per test PING</param>
|
|
/// <param name="wwwTargets">target x download www page</param>
|
|
public void scriviDiagnostica(string testoLog, string[] pingTargets, string[] wwwTargets)
|
|
{
|
|
string txtDiag = "";
|
|
try
|
|
{
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, SteamwareStrings.charLine('+', 80)));
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, SteamwareStrings.charLine('+', 80)));
|
|
scriviLog(string.Format("[{0}] - {1}{2}", tipoLog.DUMP_DIAGN, testoLog, Environment.NewLine));
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("UPTIME", Diagnostica.uptime)));
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("STORAGE", Diagnostica.diskUsage)));
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("USB", Diagnostica.usbDevices)));
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("SERIAL", Diagnostica.serialPorts)));
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("NETWORKING (interfaces)", Diagnostica.networkInterfaces)));
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
// se richiesto faccio PING test
|
|
if (pingTargets.Length > 0)
|
|
{
|
|
txtDiag = "";
|
|
foreach (string target in pingTargets)
|
|
{
|
|
txtDiag += SteamwareStrings.addLine(Diagnostica.pingIp(target));
|
|
}
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("NETWORKING (ping)", txtDiag)));
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
try
|
|
{
|
|
// se richiesto faccio PING test
|
|
if (wwwTargets.Length > 0)
|
|
{
|
|
txtDiag = "";
|
|
foreach (string target in wwwTargets)
|
|
{
|
|
txtDiag += SteamwareStrings.addLine(string.Format("WebPage Test ({2}): {0}{1}", Environment.NewLine, Diagnostica.getPageContent(target), target));
|
|
}
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, formDiagBlock("NETWORKING (www)", txtDiag)));
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
// chiudo
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, SteamwareStrings.charLine('-', 80)));
|
|
scriviLog(string.Format("[{0}]{1}{2}", tipoLog.DUMP_DIAGN, Environment.NewLine, SteamwareStrings.charLine('-', 80)));
|
|
}
|
|
/// <summary>
|
|
/// formatta un blococ di diagnostica (titolo, contenuto / eccezione)
|
|
/// </summary>
|
|
/// <param name="titolo"></param>
|
|
/// <param name="contenuto"></param>
|
|
/// <returns></returns>
|
|
protected string formDiagBlock(string titolo, string contenuto)
|
|
{
|
|
string answ = SteamwareStrings.charTitle(titolo, '-', 60);
|
|
try
|
|
{
|
|
answ += SteamwareStrings.addLine(contenuto);
|
|
}
|
|
catch
|
|
{
|
|
answ += string.Format("Errore diagnostica {0}", titolo);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region metodi privati
|
|
|
|
/// <summary>
|
|
/// fornisce il nome del file in cui loggare (ed eventualmente crea...)
|
|
/// </summary>
|
|
protected void newLogfileName()
|
|
{
|
|
DateTime CurrentDateTime = DateTime.Now;
|
|
_logfileName = _logBaseDir + String.Format("{0}.txt", CurrentDateTime.ToString("yyyyMMdd"));
|
|
}
|
|
/// <summary>
|
|
/// provvede a verificare la dim della cartella dei log e cancella i + vecchi fino a restare a dim inferiori a _logMaxMb
|
|
/// </summary>
|
|
protected void shrinkDir()
|
|
{
|
|
// ottengo elenco files *.txt
|
|
fileMover.obj.setDirectory(_logBaseDir);
|
|
FileInfo[] _fis = fileMover.obj.elencoFiles_FI("*.txt");
|
|
foreach (FileInfo _file in _fis)
|
|
{
|
|
if (_file.CreationTime < DateTime.Now.AddDays(-2)) // zippo files + vecchi di 2 gg...
|
|
{
|
|
fileMover.obj.zippaSingoloFile(_file);
|
|
//fileMover.obj.zippaSingoloFile(_file.Name);
|
|
// cancello l'originale...
|
|
fileMover.obj.eliminaFile(_file);
|
|
//fileMover.obj.eliminaFile(_file.Name);
|
|
}
|
|
}
|
|
// verifico directory e dimensione...
|
|
while (fileMover.obj.totalMb() > _logMaxMb)
|
|
{
|
|
// cancello i + vecchi fino a rientrare alla dimensione max...
|
|
fileMover.obj.deleteOldest();
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// fornisce il file + vecchio
|
|
/// </summary>
|
|
/// <param name="_di"></param>
|
|
/// <returns></returns>
|
|
protected void deleteOldest(DirectoryInfo _di)
|
|
{
|
|
FileInfo[] _fis = _di.GetFiles();
|
|
DateTime _oldest = DateTime.Now;
|
|
string _nome = "";
|
|
foreach (FileInfo _file in _fis)
|
|
{
|
|
if (_file.CreationTime < _oldest)
|
|
{
|
|
_nome = _file.Name;
|
|
}
|
|
}
|
|
FileInfo fi = new FileInfo(_nome);
|
|
fi.Delete();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
/// <summary>
|
|
/// tipo di log ammesso
|
|
/// </summary>
|
|
public enum tipoLog
|
|
{
|
|
/// <summary>
|
|
/// informazioni di debug
|
|
/// </summary>
|
|
DEBUG,
|
|
/// <summary>
|
|
/// dump diagnostica
|
|
/// </summary>
|
|
DUMP_DIAGN,
|
|
/// <summary>
|
|
/// errori
|
|
/// </summary>
|
|
ERROR,
|
|
/// <summary>
|
|
/// eccezioni nell'esecuzione try/catch
|
|
/// </summary>
|
|
EXCEPTION,
|
|
/// <summary>
|
|
/// errori fatali
|
|
/// </summary>
|
|
FATAL,
|
|
/// <summary>
|
|
/// informazioni opzionali
|
|
/// </summary>
|
|
INFO,
|
|
/// <summary>
|
|
/// log dei lemmi invocati per traduzione da vocabolario
|
|
/// </summary>
|
|
LEMMA,
|
|
/// <summary>
|
|
/// fase di avvio componente
|
|
/// </summary>
|
|
STARTUP,
|
|
/// <summary>
|
|
/// avvisi
|
|
/// </summary>
|
|
WARNING
|
|
}
|
|
}
|