Modifica classe logger

x evitare sovrapposizioni thread x MP-IO
This commit is contained in:
Samuele E. Locatelli
2018-11-06 10:59:13 +01:00
parent bf8e810f27
commit 0798b5ee61
+50 -11
View File
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
namespace SteamWare
{
@@ -10,8 +11,6 @@ namespace SteamWare
{
#region dichiarazione variabili
#region altri dati
/// <summary>
/// directory base x logs
/// </summary>
@@ -28,9 +27,14 @@ namespace SteamWare
/// controlla se si debba mantenere sotto controllo la dimensioen della cartella logs
/// </summary>
protected bool _doShrinkFolder;
#endregion
/// <summary>
/// Ultima verifica directory (x shrink)
/// </summary>
private static DateTime lastDirCheck;
/// <summary>
/// metodo gestione wirteLock su file log
/// </summary>
private static ReaderWriterLockSlim _readWriteLock = new ReaderWriterLockSlim();
#endregion
@@ -63,6 +67,8 @@ namespace SteamWare
{
_doShrinkFolder = true;
}
// all'avvio imposto ultimo check a 23 ore fa... così farà shrink DOPO 1 h da avvio
lastDirCheck = DateTime.Now.AddHours(-23);
}
/// <summary>
/// livello di log applicazione (da web.config, chiave '_logLevel')
@@ -181,25 +187,30 @@ namespace SteamWare
return fatto;
}
/// <summary>
/// Vera chaimata scrittura log x override con gestione REDIS di calmierazione
/// Vera chiamata scrittura log x override con gestione REDIS di calmierazione
/// </summary>
/// <param name="_testoPre"></param>
/// <returns></returns>
private bool doScriviLog(string _testoPre)
{
bool fatto = false;
// 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)
// se è configurato x shrink ed è passato almeno 1 gg da ultimo check...
if (_doShrinkFolder && lastDirCheck.AddDays(1) < DateTime.Now)
{
// verifica dim directory ed eventualmente cancella... - opzionale
shrinkDir();
// salvo nuova ora di check
lastDirCheck = DateTime.Now;
}
// (ri)genera il nome del file di log...
newLogfileName();
// scrivo thread safe
fatto = WriteToFileThreadSafe(_logfileName, _testo);
#if false
// aggiunge in append...
StreamWriter sw = new StreamWriter(_logfileName, true);
bool fatto;
try
{
sw.WriteLine(_testo);
@@ -210,11 +221,39 @@ namespace SteamWare
catch
{
fatto = false;
}
}
#endif
return fatto;
}
/// <summary>
/// Esecuzione scrittura ThreadSafe
/// </summary>
/// <param name="text2write"></param>
/// <param name="logPath"></param>
public bool WriteToFileThreadSafe(string logPath, string text2write)
{
bool answ = false;
// Set Status to Locked
_readWriteLock.EnterWriteLock();
try
{
// Append text to the file
using (StreamWriter sw = File.AppendText(logPath))
{
sw.WriteLine(text2write);
sw.Close();
answ = true;
}
}
finally
{
// Release lock
_readWriteLock.ExitWriteLock();
}
return answ;
}
/// <summary>
/// scrive un messaggio di log con etichetta pre
/// </summary>