409699e76c
- iobConfig - utils - BaseObj
400 lines
14 KiB
C#
400 lines
14 KiB
C#
using IOB_UT_NEXT;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Threading;
|
|
using static IOB_UT_NEXT.BaseAlarmConf;
|
|
|
|
namespace IOB_WIN_NEXT.Iob
|
|
{
|
|
/// <summary>
|
|
/// Classe di base per IOB
|
|
/// </summary>
|
|
public class BaseObj : IOB_UT_NEXT.Iob.BaseObj
|
|
{
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// Form chiamante
|
|
/// </summary>
|
|
protected AdapterForm parentForm;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Effettua logging DEBUG corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
protected void lgDebug(string message, bool sendToForm = true)
|
|
{
|
|
bool doVeto = checkLogVeto(20, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Debug(message);
|
|
if (sendToForm)
|
|
{
|
|
sendToLogWatch("DEBUG", message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging DEBUG corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgDebug(string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(20, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Debug(message, args);
|
|
sendToLogWatch("DEBUG", message, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging ERROR corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
protected void lgError(string message, bool sendToForm = true)
|
|
{
|
|
bool doVeto = checkLogVeto(2, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Error(message);
|
|
if (sendToForm)
|
|
{
|
|
sendToLogWatch("ERROR", message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging ERROR corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgError(string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(2, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Error(message, args);
|
|
sendToLogWatch("ERROR", message, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging ERROR corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="exception"></param>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgError(Exception exception, string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(2, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Error(exception, message, args);
|
|
sendToLogWatch("ERROR", message, exception, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging FATAL corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
protected void lgFatal(string message, bool sendToForm = true)
|
|
{
|
|
bool doVeto = checkLogVeto(1, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Fatal(message);
|
|
if (sendToForm)
|
|
{
|
|
sendToLogWatch("FATAL", message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging FATAL corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgFatal(string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(1, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Fatal(message, args);
|
|
sendToLogWatch("FATAL", message, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging FATAL corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="exception"></param>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgFatal(Exception exception, string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(1, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Fatal(exception, message, args);
|
|
sendToLogWatch("FATAL", message, exception, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
protected void lgInfo(string message, bool sendToForm = true)
|
|
{
|
|
bool doVeto = checkLogVeto(30, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Info(message);
|
|
if (sendToForm)
|
|
{
|
|
sendToLogWatch("INFO", message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgInfo(string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(30, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Info(message, args);
|
|
sendToLogWatch("INFO", message, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="sendToForm"></param>
|
|
protected void lgInfoStartup(string message, bool sendToForm = true)
|
|
{
|
|
bool doVeto = checkLogVeto(30, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
DateTime adesso = DateTime.Now;
|
|
if (adesso.Subtract(lastLogStartup).TotalMinutes > vetoLogStartupDuration)
|
|
{
|
|
lg.Info(message);
|
|
// se supera di 5 minutis cadenza -_> reimposto veto...
|
|
if (adesso.Subtract(lastLogStartup).TotalMinutes > vetoLogStartupDuration + 5)
|
|
{
|
|
lastLogStartup = adesso;
|
|
}
|
|
}
|
|
if (sendToForm)
|
|
{
|
|
sendToLogWatch("INFO", message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgInfoStartup(string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(30, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
DateTime adesso = DateTime.Now;
|
|
if (adesso.Subtract(lastLogStartup).TotalMinutes > vetoLogStartupDuration)
|
|
{
|
|
lg.Info(message, args);
|
|
// se supera di 5 minutis cadenza -_> reimposto veto...
|
|
if (adesso.Subtract(lastLogStartup).TotalMinutes > vetoLogStartupDuration + 5)
|
|
{
|
|
lastLogStartup = adesso;
|
|
}
|
|
}
|
|
sendToLogWatch("INFO", message, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
protected void lgTrace(string message, bool sendToForm = true)
|
|
{
|
|
bool doVeto = checkLogVeto(60, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Trace(message);
|
|
if (sendToForm)
|
|
{
|
|
sendToLogWatch("TRACE", message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void lgTrace(string message, params object[] args)
|
|
{
|
|
bool doVeto = checkLogVeto(60, ref message);
|
|
// se non ho veto --> loggo
|
|
if (!doVeto)
|
|
{
|
|
lg.Factory.Configuration.Variables["codIOB"] = cIobConf.codIOB;
|
|
lg.Trace(message, args);
|
|
sendToLogWatch("TRACE", message, args);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invia messaggio a logWatcher
|
|
/// </summary>
|
|
/// <param name="messType"></param>
|
|
/// <param name="message"></param>
|
|
protected void sendToLogWatch(string messType, string message)
|
|
{
|
|
newDisplayData currDispData = new newDisplayData();
|
|
currDispData.newLiveLogData = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} | {messType} | {message}";
|
|
parentForm.updateFormDisplay(currDispData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invia messaggio a logWatcher
|
|
/// </summary>
|
|
/// <param name="messType"></param>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void sendToLogWatch(string messType, string message, params object[] args)
|
|
{
|
|
try
|
|
{
|
|
string expString = string.Format(message, args);
|
|
newDisplayData currDispData = new newDisplayData();
|
|
currDispData.newLiveLogData = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} | {messType} | {expString}";
|
|
parentForm.updateFormDisplay(currDispData);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invia messaggio a logWatcher
|
|
/// </summary>
|
|
/// <param name="messType"></param>
|
|
/// <param name="exception"></param>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
protected void sendToLogWatch(string messType, string message, Exception exception, params object[] args)
|
|
{
|
|
try
|
|
{
|
|
string expString = string.Format(message, args);
|
|
newDisplayData currDispData = new newDisplayData();
|
|
currDispData.newLiveLogData = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} | {messType} | {expString}{Environment.NewLine}{exception}";
|
|
parentForm.updateFormDisplay(currDispData);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Verifica se ci sia veto log attivo e gestisce casistiche
|
|
/// </summary>
|
|
/// <param name="vetoSec"></param>
|
|
/// <param name="message"></param>
|
|
/// <returns></returns>
|
|
private bool checkLogVeto(int vetoSec, ref string message)
|
|
{
|
|
//verifico SE si debba fare log, altrimenti metto in coda...
|
|
bool doVeto = true;
|
|
DateTime adesso = DateTime.Now;
|
|
if (VetoLog.ContainsKey(message))
|
|
{
|
|
// conteggio num veto a +1...
|
|
if (VetoLogCount.ContainsKey(message))
|
|
{
|
|
VetoLogCount[message]++;
|
|
}
|
|
else
|
|
{
|
|
VetoLogCount.Add(message, 1);
|
|
}
|
|
// controllo scadenza, quando superata soglia aggiorno messaggio con {n} x {messaggio}
|
|
if (adesso.Subtract(VetoLog[message]).TotalSeconds > vetoSec)
|
|
{
|
|
doVeto = false;
|
|
string newMessage = $"{VetoLogCount[message]} x {message}";
|
|
VetoLog.Remove(message);
|
|
VetoLogCount.Remove(message);
|
|
message = newMessage;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// primo --> loggo
|
|
doVeto = false;
|
|
VetoLog.Add(message, adesso.AddSeconds(vetoSec));
|
|
VetoLogCount.Add(message, 0);
|
|
}
|
|
// restituisco esito!
|
|
return doVeto;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |