Files
mapo-mono/MP.MONO.Data/Controllers/MpDbController.cs
T
Samuele Locatelli e71b873a4c - nuget update
- pulizia codice da warning
2023-08-21 18:06:54 +02:00

1787 lines
69 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MP.MONO.Core.DTO;
using MP.MONO.Data.DbModels;
using MP.MONO.Data.DTO;
using NLog;
using System.Diagnostics;
using static MP.MONO.Core.Enums;
namespace MP.MONO.Data.Controllers
{
public class MpDbController : IDisposable
{
#region Public Constructors
public MpDbController(IConfiguration currAppConf)
{
appConf = currAppConf;
LogDurationLimitMs = appConf.GetValue<int>("OptPar:LogDurationLimitMs");
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Elenco records anagrafica allarmi
/// </summary>
/// <returns></returns>
public List<AlarmListModel> AlarmListGetAll()
{
List<AlarmListModel>? dbResult = new List<AlarmListModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
try
{
dbResult = localDbCtx
.DbSetAlarmList
.OrderBy(x => x.FullValue)
.ToList();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmListGetAll", exc);
}
LogDebug("AlarmListGetAll", stopWatch.Elapsed);
stopWatch.Stop();
}
return dbResult;
}
/// <summary>
/// Inserimento di un record anagrafica allarmi (o se esiste restituisce quello esistente)
/// </summary>
/// <param name="newItem">Record da inserire (senza ID...)</param>
/// <returns></returns>
public AlarmListModel? AlarmListInsert(string alarmText)
{
AlarmListModel? foundRec = new AlarmListModel();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// verifico che NON esista con pèari descrizione...
foundRec = localDbCtx
.DbSetAlarmList
.FirstOrDefault(x => x.FullValue == alarmText);
// se non c'è aggiungo
if (foundRec == null)
{
foundRec = new AlarmListModel()
{
FullValue = alarmText
};
localDbCtx
.DbSetAlarmList
.Add(foundRec);
localDbCtx.SaveChanges();
// rifaccio search
foundRec = localDbCtx
.DbSetAlarmList
.FirstOrDefault(x => x.FullValue == alarmText);
}
LogDebug("AlarmListInsert", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmListInsert", exc);
}
}
return foundRec;
}
/// <summary>
/// Inserimento di una lista record anagrafica allarmi
/// </summary>
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> AlarmListInsertMany(List<AlarmListModel> newItems)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await localDbCtx
.DbSetAlarmList
.AddRangeAsync(newItems);
await localDbCtx.SaveChangesAsync();
fatto = true;
stopWatch.Stop();
LogDebug("AlarmListInsertMany", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmListInsertMany", exc);
}
}
return fatto;
}
/// <summary>
/// Recupero elenco allarmi (ultimi dato "skip")
/// </summary>
/// <param name="MachineId"></param>
/// <param name="skipRec"></param>
/// <param name="numRec"></param>
/// <returns></returns>
public async Task<List<AlarmLogModel>> AlarmLogGetFilt(int MachineId, int skipRec, int numRec)
{
List<AlarmLogModel> dbResult = new List<AlarmLogModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = await localDbCtx
.DbSetAlarmLog
.AsNoTracking()
.Where(x => x.MachineId == MachineId)
.Include(m => m.MachineNav)
.OrderByDescending(x => x.AlarmLogId)
.Skip(skipRec)
.Take(numRec)
.ToListAsync();
LogDebug("AlarmLogGetFilt", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmLogGetFilt", exc);
}
}
//await Task.Delay(1);
return dbResult;
}
/// <summary> Recupero elenco allarmi con data <= alla data di riferimento (ultimi dato
/// "skip") </summary> <param name="MachineId"></param> <param name="dtRif"></param> <param
/// name="skipRec"></param> <param name="numRec"></param> <returns></returns>
public List<AlarmLogModel> AlarmLogGetFiltByDate(int MachineId, DateTime dtRif, int skipRec, int numRec)
{
List<AlarmLogModel> dbResult = new List<AlarmLogModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetAlarmLog
.AsNoTracking()
.Where(x => (x.MachineId == MachineId) && (x.DtRif <= dtRif))
.Include(m => m.MachineNav)
.OrderByDescending(x => x.AlarmLogId)
.Skip(skipRec)
.Take(numRec)
.ToList();
LogDebug("AlarmLogGetFiltByDate", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmLogGetFiltByDate", exc);
}
}
return dbResult;
}
/// <summary>
/// Inserimento di un record AlarmLog
/// </summary>
/// <param name="newItem">Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> AlarmLogInsert(AlarmLogModel newItem)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
localDbCtx
.DbSetAlarmLog
.Add(newItem);
await localDbCtx.SaveChangesAsync();
fatto = true;
LogDebug("AlarmLogInsert", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmLogInsert", exc);
}
}
return fatto;
}
/// <summary>
/// Inserimento di una lista record AlarmLog
/// </summary>
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> AlarmLogInsertMany(List<AlarmLogModel> newItems)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
if (newItems.Count > 0)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await localDbCtx
.DbSetAlarmLog
.AddRangeAsync(newItems);
await localDbCtx.SaveChangesAsync();
fatto = true;
LogInfo($"AlarmLogInsertMany| DB insert | {newItems.Count} rec", stopWatch.Elapsed);
stopWatch.Stop();
}
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmLogInsertMany", exc);
}
}
return fatto;
}
/// <summary>
/// Chiude ultimo record dato Id (se fossero + di 1 chiude tutti...)
/// </summary>
/// <param name="AlarmId">Id Allarme</param>
/// <returns></returns>
public async Task<bool> AlarmRecCloseActive(int AlarmId)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DateTime adesso = DateTime.Now;
// prendo ultimi 10000 allarmi (odierni)
var lastAlarms = localDbCtx
.DbSetAlarmRec
.OrderByDescending(x => x.AlarmRecId)
.Take(10000)
.ToList();
var candList = lastAlarms
.Where(x => x.AlarmId == AlarmId && x.DtStart >= x.DtEnd)
.ToList();
if (candList.Count > 0)
{
foreach (var item in candList)
{
item.DtEnd = adesso;
}
await localDbCtx.SaveChangesAsync();
fatto = true;
LogDebug($"AlarmRecCloseActive | {candList.Count} closed", stopWatch.Elapsed);
}
else
{
LogDebug($"AlarmRecCloseActive | NONE", stopWatch.Elapsed);
}
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecCloseActive", exc);
}
}
return fatto;
}
/// <summary>
/// Chiude ultimo record di allarmi aperti (avendo verificato sia zero da un pò...)
/// </summary>
/// <returns></returns>
public async Task<bool> AlarmRecCloseStarving()
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DateTime adesso = DateTime.Now;
var lastAlarms = localDbCtx
.DbSetAlarmRec
.OrderByDescending(x => x.AlarmRecId)
.Take(100000)
.ToList();
var candList = lastAlarms
.Where(x => x.DtStart >= x.DtEnd)
.ToList();
foreach (var item in candList)
{
item.DtEnd = adesso;
}
await localDbCtx.SaveChangesAsync();
fatto = true;
stopWatch.Stop();
LogDebug($"AlarmRecCloseStarving", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecCloseStarving", exc);
}
}
return fatto;
}
/// <summary>
/// Recupero elenco allarmi Rec ATTIVI (non chiusi)
/// </summary>
/// <returns></returns>
public List<AlarmRecModel> AlarmRecGetActive()
{
List<AlarmRecModel> dbResult = new List<AlarmRecModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// recupero 10'000 allarme x limitare...
int numRec = localDbCtx
.DbSetAlarmRec
.Count();
int numMax = 10000;
int limitIdx = 0;
if (numRec > numMax)
{
var limitRec = localDbCtx
.DbSetAlarmRec
.AsNoTracking()
.OrderByDescending(x => x.AlarmRecId)
.Skip(numMax)
.Take(1)
.FirstOrDefault();
limitIdx = limitRec != null ? limitRec.AlarmRecId : 0;
}
dbResult = localDbCtx
.DbSetAlarmRec
//.AsNoTracking()
.Where(x => x.AlarmRecId > limitIdx && x.DtStart >= x.DtEnd)
.Include(m => m.AlarmListNav)
.ToList();
LogDebug("AlarmRecGetActive", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecGetActive", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero elenco allarmi Rec (ultimi dato "skip")
/// </summary>
/// <param name="MachineId"></param>
/// <param name="skipRec"></param>
/// <param name="numRec"></param>
/// <returns></returns>
public async Task<List<AlarmRecModel>> AlarmRecGetFilt(int MachineId, int skipRec, int numRec)
{
List<AlarmRecModel> dbResult = new List<AlarmRecModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = await localDbCtx
.DbSetAlarmRec
.AsNoTracking()
.Where(x => x.MachineId == MachineId)
.Include(m => m.MachineNav)
.Include(m => m.AlarmListNav)
.OrderByDescending(x => x.DtStart)
.Skip(skipRec)
.Take(numRec)
.ToListAsync();
stopWatch.Stop();
LogDebug("AlarmRecGetFilt", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecGetFilt", exc);
}
}
return dbResult;
}
/// <summary> Recupero elenco allarmi Rec con data <= a quella di riferimento(ultimi dato
/// "skip") </summary> <param name="MachineId"></param> <param name="dtRif"></param> <param
/// name="skipRec"></param> <param name="numRec"></param> <returns></returns>
public List<AlarmRecModel> AlarmRecGetFiltByDate(int MachineId, DateTime dtRif, int skipRec, int numRec)
{
List<AlarmRecModel> dbResult = new List<AlarmRecModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetAlarmRec
.AsNoTracking()
.Where(x => (x.MachineId == MachineId) && (x.DtStart <= dtRif))
.Include(m => m.MachineNav)
.Include(m => m.AlarmListNav)
.OrderByDescending(x => x.DtStart)
.Skip(skipRec)
.Take(numRec)
.ToList();
LogDebug("AlarmRecGetFiltByDate", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecGetFiltByDate", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero lista sommatoria durata eventi allarme per analisi, ordinati in logica di
/// pareto (Max-min)
/// </summary>
/// <param name="machineId">Id Macchina</param>
/// <param name="dtFrom">inizio analisi (evento deve iniziare/finire entro periodo)</param>
/// <param name="dtTo">fine analisi (evento deve iniziare/finire entro periodo)</param>
/// <param name="minDur">durata minima evento per venire considerato, -1 = tutti</param>
/// <param name="maxDur">durata massima evento per venire considerato, -1 = tutti</param>
/// <returns></returns>
public List<AlarmDurationDTO> AlarmRecGetParetoDur(int machineId, DateTime dtFrom, DateTime dtTo, int minDur = -1, int maxDur = -1)
{
List<AlarmDurationDTO> dbResult = new List<AlarmDurationDTO>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// effettuo il calcolo in + step...
var rawData = localDbCtx
.DbSetAlarmRec
.AsNoTracking()
.Where(x => x.MachineId == machineId
&& (x.DtStart >= dtFrom && x.DtStart <= dtTo)
&& (x.Duration >= minDur || minDur < 0)
&& (x.Duration <= maxDur || maxDur < 0))
.ToList();
dbResult = rawData
.GroupBy(g => g.AlarmId)
.Select(g => new AlarmDurationDTO() { MachineId = machineId, AlarmId = g.Key, Duration = g.Sum(x => x.Duration) })
.ToList()
.Join(
AlarmListGetAll(),
f => f.AlarmId,
l => l.AlarmId, (dur, list) => new AlarmDurationDTO()
{
AlarmId = dur.AlarmId,
Duration = dur.Duration,
MachineId = dur.MachineId,
AlarmDescription = list.FullValue
}
)
.OrderByDescending(o => o.Duration)
.ToList();
LogDebug("AlarmRecGetParetoDur", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecGetParetoDur", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero lista di conteggio eventi allarme per analisi, ordinati in logica di pareto (Max-min)
/// </summary>
/// <param name="machineId">Id Macchina</param>
/// <param name="dtFrom">inizio analisi (evento deve iniziare DOPO questa data)</param>
/// <param name="dtTo">fine analisi (evento deve iniziare prima di questa data)</param>
/// <param name="minDur">durata minima evento per venire considerato, -1 = tutti</param>
/// <param name="maxDur">durata massima evento per venire considerato, -1 = tutti</param>
/// <returns></returns>
public List<AlarmFreqDTO> AlarmRecGetParetoFreq(int machineId, DateTime dtFrom, DateTime dtTo, int minDur = -1, int maxDur = -1)
{
List<AlarmFreqDTO> dbResult = new List<AlarmFreqDTO>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetAlarmRec
.AsNoTracking()
.Where(x => x.MachineId == machineId
&& (x.DtStart >= dtFrom && x.DtStart <= dtTo)
&& (x.Duration >= minDur || minDur < 0)
&& (x.Duration <= maxDur || maxDur < 0))
.GroupBy(g => g.AlarmId)
.Select(g => new AlarmFreqDTO() { MachineId = machineId, AlarmId = g.Key, EventCount = g.Count() })
.ToList()
.Join(
AlarmListGetAll(),
f => f.AlarmId,
l => l.AlarmId, (freq, list) => new AlarmFreqDTO()
{
AlarmId = freq.AlarmId,
EventCount = freq.EventCount,
MachineId = freq.MachineId,
AlarmDescription = list.FullValue
}
)
.OrderByDescending(o => o.EventCount)
.ToList();
LogDebug("AlarmRecGetParetoFreq", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecGetParetoFreq", exc);
}
}
return dbResult;
}
/// <summary>
/// Inserimento di un record AlarmRec
/// </summary>
/// <param name="newItem">Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> AlarmRecInsert(AlarmRecModel newItem)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// verifico non sia già aperto... verifico eventuali items già aperti da NON
// dover aggiungere...
var stillOpenAlarms = localDbCtx
.DbSetAlarmRec
.Where(x => x.DtStart >= x.DtEnd && x.AlarmId == newItem.AlarmId)
.Select(x => x.AlarmId)
.ToList();
// se ho trovato --> salto!
if (stillOpenAlarms != null && stillOpenAlarms.Count > 0)
{
LogError($"Errore: trovato {stillOpenAlarms.Count} istanze già attive | Id: {newItem.AlarmId}");
}
else
{
localDbCtx
.DbSetAlarmRec
.Add(newItem);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
LogInfo($"AlarmRecInsert| DB insert 1 rec", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecInsert", exc);
}
}
return fatto;
}
/// <summary>
/// Inserimento di una lista record AlarmRec
/// </summary>
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
/// <returns></returns>
public bool AlarmRecInsertMany(List<AlarmRecModel> newItems)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
if (newItems.Count > 0)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// aggiungo
localDbCtx
.DbSetAlarmRec
.AddRange(newItems);
// salvo!
localDbCtx.SaveChanges();
fatto = true;
LogInfo($"AlarmRecInsertMany | DB insert | {newItems.Count} rec", stopWatch.Elapsed);
stopWatch.Stop();
}
}
catch (Exception exc)
{
LogException("Eccezione durante AlarmRecInsertMany", exc);
}
}
return fatto;
}
/// <summary>
/// Recupero counters da DB
/// </summary>
/// <returns></returns>
public List<CounterModel> CountersGetAll()
{
List<CounterModel> dbResult = new List<CounterModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetCounter
.AsNoTracking()
.ToList();
LogDebug($"CountersGetAll", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante CountersGetAll", exc);
}
}
return dbResult;
}
/// <summary>
/// Inserimento/Update di una lista counters
/// </summary>
/// <param name="newItems">Lista Record da inserire</param>
/// <returns></returns>
public bool CountersUpsertMany(List<CounterModel> newItems)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
if (newItems.Count > 0)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// x ogni valore cerco...
foreach (var item in newItems)
{
var oldRec = localDbCtx
.DbSetCounter
.Where(x => x.CCode == item.CCode)
.FirstOrDefault();
if (oldRec == null)
{
localDbCtx
.DbSetCounter
.Add(item);
}
else
{
oldRec.ActualVal = item.ActualVal;
localDbCtx.Entry(oldRec).State = EntityState.Modified;
}
}
// salvo le modifiche
localDbCtx.SaveChanges();
fatto = true;
stopWatch.Stop();
LogInfo($"CountersUpsertMany | DB insert | {newItems.Count} rec", stopWatch.Elapsed);
}
}
catch (Exception exc)
{
LogException("Eccezione durante CountersUpsertMany", exc);
}
}
return fatto;
}
/// <summary>
/// Recupero DataLogDTO data condizione filtro
/// </summary>
/// <param name="machineId">ID macchina</param>
/// <param name="dataType">Tipologia di dato richiesto da enum</param>
/// <param name="fluxType">Tipo di Flusso</param>
/// <param name="inizio">DT Inizio estrazione</param>
/// <param name="fine">DT Fine estrazione</param>
/// <returns></returns>
public async Task<List<DataLogDTO>> DataLogDtoGetFilt(int machineId, DataLogType dataType, string fluxType, DateTime inizio, DateTime fine)
{
List<DataLogDTO> dbResult = new List<DataLogDTO>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = await localDbCtx
.DbSetDataLog
.Where(x => (x.MachineId == machineId) && (x.FluxType == fluxType) && (x.DataType == dataType || dataType == DataLogType.ND) && (x.DtRif >= inizio && x.DtRif <= fine))
.Select(r => new DataLogDTO() { DataLogId = r.DataLogId, DtRif = r.DtRif, ValNum = r.ValNum })
.OrderByDescending(x => x.DataLogId)
.ToListAsync();
stopWatch.Stop();
LogDebug("DataLogDtoGetFilt", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante DataLogDtoGetFilt", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero record DataLog data condizione filtro
/// </summary>
/// <param name="dataType">Tipologia di dato richiesto da enum</param>
/// <param name="inizio"></param>
/// <param name="fine"></param>
/// <param name="machineId"></param>
/// <param name="fluxType"></param>
/// <returns></returns>
public async Task<List<DataLogModel>> DataLogGetFilt(DataLogType dataType, DateTime inizio, DateTime fine, int machineId, string fluxType)
{
List<DataLogModel> dbResult = new List<DataLogModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = await localDbCtx
.DbSetDataLog
.Where(x => (x.MachineId == machineId) && (x.FluxType == fluxType) && (x.DataType == dataType || dataType == DataLogType.ND) && (x.DtRif >= inizio && x.DtRif <= fine))
.Include(m => m.MachineNav)
.OrderByDescending(x => x.DataLogId)
.ToListAsync();
stopWatch.Stop();
LogDebug("DataLogGetFilt (5p)", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante DataLogGetFilt (5p)", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero record DataLog data condizione filtro (multi-flusso)
/// </summary>
/// <param name="dataType">Tipologia di dato richiesto da enum</param>
/// <param name="inizio"></param>
/// <param name="fine"></param>
/// <returns></returns>
public async Task<List<DataLogModel>> DataLogGetFilt(DataLogType dataType, DateTime inizio, DateTime fine)
{
List<DataLogModel> dbResult = new List<DataLogModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = await localDbCtx
.DbSetDataLog
.Where(x => (x.DataType == dataType || dataType == DataLogType.ND) && (x.DtRif >= inizio && x.DtRif <= fine))
.Include(m => m.MachineNav)
.OrderByDescending(x => x.DataLogId)
.ToListAsync();
stopWatch.Stop();
LogDebug("DataLogGetFilt (3p)", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante DataLogGetFilt (3p)", exc);
}
}
return dbResult;
}
/// <summary>
/// Inserimento di un SET di record DataLog (post aggregazione base VC)
/// </summary>
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> DataLogInsertMany(List<DataLogModel> newItems)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await localDbCtx
.DbSetDataLog
.AddRangeAsync(newItems);
await localDbCtx.SaveChangesAsync();
fatto = true;
stopWatch.Stop();
LogDebug("DataLogInsertMany", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante DataLogInsertMany", exc);
}
}
return fatto;
}
/// <summary>
/// Recupero record DataStAg (aggregati) data condizione filtro
/// </summary>
/// <param name="MachineId"></param>
/// <param name="FluxType"></param>
/// <param name="skipRec"></param>
/// <param name="numRec"></param>
/// <returns></returns>
public List<DataStAgModel> DataStAgGetFilt(int MachineId, string FluxType, int skipRec, int numRec)
{
List<DataStAgModel> dbResult = new List<DataStAgModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetDataStAg
.Where(x => x.MachineId == MachineId && x.FluxType == FluxType)
.Include(m => m.MachineNav)
.OrderByDescending(x => x.DataStAgId)
.Skip(skipRec)
.Take(numRec)
.ToList();
stopWatch.Stop();
LogDebug("DataStAgGetFilt", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante DataStAgGetFilt", exc);
}
}
return dbResult;
}
/// <summary>
/// Inserimento di un record DataStAg (aggregati) - creati da ANALYZER che effettua compattazione
/// </summary>
/// <param name="newItem">Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> DataStAgInsert(DataStAgModel newItem)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
localDbCtx
.DbSetDataStAg
.Add(newItem);
await localDbCtx.SaveChangesAsync();
fatto = true;
stopWatch.Stop();
LogDebug("DataStAgInsert", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante DataStAgInsert", exc);
}
}
return fatto;
}
public void Dispose()
{
// Clear database context
Log.Info("Dispose di MpDbController");
}
public ProductionDTO MachineGetProd()
{
// !!!FIXME TODO... è fake...
Random rand = new Random();
int stdCycle = 5;
ProductionDTO currMachDto = new ProductionDTO()
{
Order = "ODL Test",
ItemCode = "ART.0000123",
ProgName = "P000012",
CurrQty = DateTime.Now.Minute + rand.Next(1, 40),
OrderQty = 100,
CycleTimeMin = rand.NextDouble() * stdCycle,
Message = "...simulated data..."
};
Task.Delay(400).Wait();
return currMachDto;
}
/// <summary>
/// Recupero pareto status impianto
/// </summary>
/// <param name="MachineId">Macchina selezionata</param>
/// <param name="from">inizio periodo</param>
/// <param name="to">fine periodo</param>
/// <returns></returns>
public List<ParetoStatusModel> ParetoStatusMach(int MachineId, DateTime from, DateTime to)
{
List<ParetoStatusModel> dbResult = new List<ParetoStatusModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetParetoStatus
.FromSqlRaw<ParetoStatusModel>("CALL stp_paretoStatus({0},{1},{2});", MachineId, from.ToString("yyyy-MM-dd"), to.ToString("yyyy-MM-dd"))
.ToList();
LogDebug("ParetoStatusMach", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante ParetoStatusMach", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupera elenco contatori ammessi
/// </summary>
/// <returns></returns>
public List<CounterModel> PMCounterModelGetAll()
{
List<CounterModel> dbResult = new List<CounterModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetCounter
.ToList();
LogDebug("PMCounterModelGetAll", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante PMCounterModelGetAll", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupera elenco Gruppi Macchina PM ammessi
/// </summary>
/// <returns></returns>
public List<PMMGroupModel> PMMachGroupGetAll()
{
List<PMMGroupModel> dbResult = new List<PMMGroupModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetPMMachGroup
.ToList();
LogDebug("PMMachGroupGetAll", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante PMMachGroupGetAll", exc);
}
}
return dbResult;
}
/// <summary>
/// Eliminazione Record
/// </summary>
/// <param name="currRec"></param>
/// <param name="forceDelete"></param>
/// <returns></returns>
public async Task<bool> PMTaskDeleteRecord(PrevMaintTaskModel currRec, bool forceDelete)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var currVal = localDbCtx
.DbSetPMTask
.Where(x => x.PMTaskId == currRec.PMTaskId)
.FirstOrDefault();
if (currVal != null)
{
// se effettivo delete
if (forceDelete)
{
localDbCtx
.DbSetPMTask
.Remove(currVal);
}
else
{
currVal.IsDisabled = true;
}
await localDbCtx.SaveChangesAsync();
}
fatto = true;
stopWatch.Stop();
LogDebug("PMTaskDeleteRecord", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante PMTaskDeleteRecord", exc);
}
}
return fatto;
}
/// <summary>
/// Riabilitazione task disabilitato
/// </summary>
/// <param name="currRec"></param>
/// <returns></returns>
public async Task<bool> PMTaskEnableRecord(PrevMaintTaskModel currRec)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var currVal = localDbCtx
.DbSetPMTask
.Where(x => x.PMTaskId == currRec.PMTaskId)
.FirstOrDefault();
if (currVal != null)
{
// se effettivo delete
if (currRec.IsDisabled)
{
currVal.IsDisabled = false;
}
await localDbCtx.SaveChangesAsync();
}
fatto = true;
LogDebug("PMTaskEnableRecord", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante PMTaskEnableRecord", exc);
}
}
return fatto;
}
/// <summary>
/// Recupera elenco Topics PM ammessi
/// </summary>
/// <returns></returns>
public List<PMTaskTopicModel> PMTaskTopicGetAll()
{
List<PMTaskTopicModel> dbResult = new List<PMTaskTopicModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetPMTopic
.ToList();
LogDebug("PMTaskTopicGetAll", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante PMTaskTopicGetAll", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupera elenco User Team PM ammessi
/// </summary>
/// <returns></returns>
public List<PMUTModel> PMUTeamGetAll()
{
List<PMUTModel> dbResult = new List<PMUTModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetUTeam
.ToList();
stopWatch.Stop();
LogDebug("PMUTeamGetAll", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante PMUTeamGetAll", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero elenco Task Manut Prev Previsti/Configurati (ultimi dato "skip")
/// </summary>
/// <param name="MachineId"></param>
/// <param name="skipRec"></param>
/// <param name="numRec"></param>
/// <returns></returns>
public List<PrevMaintTaskModel> PrevMaintTaskGetFilt(int MachineId, int skipRec, int numRec)
{
List<PrevMaintTaskModel> dbResult = new List<PrevMaintTaskModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetPMTask
.Where(x => x.MachineId == MachineId)
.Include(m => m.CounterNav)
.Include(m => m.MachGroupNav)
.Include(m => m.MachineNav)
.Include(m => m.TopicNav)
.Include(m => m.UserTeamNav)
.OrderBy(x => x.ExtIdx)
.Skip(skipRec)
.Take(numRec)
.ToList();
LogDebug("PrevMaintTaskGetFilt", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante PrevMaintTaskGetFilt", exc);
}
}
return dbResult;
}
/// <summary>
/// Esegue update del record
/// </summary>
/// <param name="editRec"></param>
/// <returns></returns>
public async Task<bool> PrevMaintTaskUpdate(PrevMaintTaskModel editRec)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var currRec = localDbCtx
.DbSetPMTask
.Where(x => x.PMTaskId == editRec.PMTaskId)
.FirstOrDefault();
if (currRec != null)
{
currRec.CCode = editRec.CCode;
currRec.ExpiryVal = editRec.ExpiryVal;
currRec.ExtIdx = editRec.ExtIdx;
currRec.PMMGCode = editRec.PMMGCode;
currRec.PMTCode = editRec.PMTCode;
currRec.PMUTCode = editRec.PMUTCode;
currRec.JobDescription = editRec.JobDescription;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else
{
// se nuovo (user edit) --> NON protetto!
editRec.Protected = false;
localDbCtx
.DbSetPMTask
.Add(editRec);
}
await localDbCtx.SaveChangesAsync();
fatto = true;
stopWatch.Stop();
LogDebug("PrevMaintTaskUpdate", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante PrevMaintTaskUpdate", exc);
}
}
return fatto;
}
/// <summary>
/// Inserimento di una lista record Production Log
/// </summary>
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
/// <returns></returns>
public async Task<bool> ProdLogInsertMany(List<ProdLogModel> newItems)
{
bool fatto = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await localDbCtx
.DbSetProdLog
.AddRangeAsync(newItems);
await localDbCtx.SaveChangesAsync();
fatto = true;
stopWatch.Stop();
LogDebug("ProdLogInsertMany", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante ProdLogInsertMany", exc);
}
}
return fatto;
}
/// <summary>
/// Effettua chiamata stored x pulizia dati stale/vecchi
/// </summary>
/// <param name="numDay2Keep"></param>
/// <returns></returns>
public List<TaskExecModel> RemoveOldData(int numDay2Keep)
{
List<TaskExecModel> dbResult = new List<TaskExecModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
try
{
dbResult = localDbCtx
.DbSetTaskExec
.FromSqlRaw<TaskExecModel>("CALL stp_removeOldData({0});", numDay2Keep)
.ToList();
stopWatch.Stop();
LogDebug("RemoveOldData", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante RemoveOldData", exc);
}
}
return dbResult;
}
/// <summary>
/// genera elenco Task Schedulati missing da schema + attualmente presenti...
/// </summary>
/// <param name="MachineId"></param>
/// <returns></returns>
public async Task<bool> SchedMaintTaskCreateMissing(int MachineId)
{
bool answ = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
List<PrevMaintTaskModel> missTask = new List<PrevMaintTaskModel>();
// elenco dei task NON disattivati
List<PrevMaintTaskModel>? allTask = localDbCtx
.DbSetPMTask
.Where(x => x.MachineId == MachineId && !x.IsDisabled)
.Include(i => i.CounterNav)
.ToList();
// analizzo task per idx del PMTask
var reqTask = allTask
.Select(x => x.PMTaskId)
.ToList();
var openTask = localDbCtx
.DbSetSMTask
.Where(x => x.DtExecution == DateTime.MinValue)
.Include(m => m.PMTaskeNav)
.ToList();
var currTask = openTask
.Where(x => x.PMTaskeNav.MachineId == MachineId)
.Select(x => x.PMTaskId)
.ToList();
// elenco dei SOLI task presenti
var listTaskOk = currTask.Intersect(reqTask).ToList();
// dall'elenco di quelli originali elimino presenti --> ho elenco mancanti...
foreach (var iTask in allTask)
{
// se non nell'elenco aggiungo...
if (!listTaskOk.Exists(x => x == iTask.PMTaskId))
{
missTask.Add(iTask);
}
}
var newRecs = missTask.Select(x => new PendingMaintModel()
{
CCode = x.CCode,
DtCreation = DateTime.Now,
ExpiryVal = x.ExpiryVal,
PMTaskId = x.PMTaskId,
CountStartVal = x.CounterNav.ActualVal
}).ToList();
localDbCtx
.DbSetSMTask
.AddRange(newRecs);
// salvo
localDbCtx.SaveChanges();
answ = true;
LogDebug("SchedMaintTaskCreateMissing", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante SchedMaintTaskGetFilt", exc);
}
}
await Task.Delay(1);
return answ;
}
/// <summary>
/// Recupero elenco Task Schedulati dati Macchina ed intervento "master"
/// </summary>
/// <param name="MachineId">Macchina selezionata</param>
/// <param name="PMTaskId">ID del PMTask parent</param>
/// <param name="skipRec">Num record da saltare</param>
/// <param name="numRec">num rec da recuperare</param>
/// <returns></returns>
public List<PendingMaintModel> SchedMaintTaskGetByPMTask(int MachineId, int PMTaskId, int skipRec, int numRec)
{
List<PendingMaintModel> dbResult = new List<PendingMaintModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetSMTask
.Include(i => i.CounterNav)
.Where(x => x.PMTaskeNav.MachineId == MachineId && x.PMTaskId == PMTaskId && x.DtExecution > DateTime.MinValue)
.Include(m => m.PMTaskeNav.MachGroupNav)
.Include(m => m.CounterNav)
.Include(m => m.PMTaskeNav.MachineNav)
.Include(m => m.PMTaskeNav.TopicNav)
.Include(m => m.PMTaskeNav.UserTeamNav)
.OrderByDescending(x => x.DtCreation)
.Skip(skipRec)
.Take(numRec)
.ToList();
LogDebug("SchedMaintTaskGetByPMTask", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante SchedMaintTaskGetByPMTask", exc);
}
}
return dbResult;
}
/// <summary>
/// Recupero elenco Task Schedulati (ultimi dato "skip")
/// </summary>
/// <param name="MachineId">Macchina selezionata</param>
/// <param name="skipRec">Num record da saltare</param>
/// <param name="numRec">num rec da recuperare</param>
/// <returns></returns>
public List<PendingMaintModel> SchedMaintTaskGetFilt(int MachineId, int skipRec, int numRec)
{
List<PendingMaintModel> dbResult = new List<PendingMaintModel>();
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = localDbCtx
.DbSetSMTask
.Include(i => i.CounterNav)
.Where(x => x.PMTaskeNav.MachineId == MachineId && x.DtExecution == DateTime.MinValue)
.Include(m => m.PMTaskeNav.MachGroupNav)
.Include(m => m.CounterNav)
.Include(m => m.PMTaskeNav.MachineNav)
.Include(m => m.PMTaskeNav.TopicNav)
.Include(m => m.PMTaskeNav.UserTeamNav)
.OrderBy(x => x.PMTaskeNav.ExtIdx)
.Skip(skipRec)
.Take(numRec)
.ToList();
stopWatch.Stop();
LogDebug("SchedMaintTaskGetFilt", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante SchedMaintTaskGetFilt", exc);
}
}
return dbResult;
}
/// <summary>
/// Registro esecuzione task schedualto
/// </summary>
/// <param name="MachineId"></param>
/// <param name="SMTaskId"></param>
/// <param name="ExecDT"></param>
/// <param name="ExecUser"></param>
/// <returns></returns>
public async Task<bool> SchedMaintTaskSetDone(int MachineId, int SMTaskId, DateTime ExecDT, string ExecUser)
{
bool answ = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// Aggiorno il task schedulato
var dbResult = localDbCtx
.DbSetSMTask
.Include(i => i.CounterNav)
.Where(x => x.PMTaskeNav.MachineId == MachineId && x.SMTaskId == SMTaskId)
.FirstOrDefault();
if (dbResult != null)
{
// aggiorno il record
dbResult.DtExecution = ExecDT;
dbResult.UserCode = ExecUser;
dbResult.ElapsedVal = dbResult.CounterNav.ActualVal - dbResult.CountStartVal;
// aggiorno il task parent col numero di task eseguiti...
int numDone = localDbCtx
.DbSetSMTask
.Where(x => x.PMTaskId == dbResult.PMTaskId)
.Count();
var parentRecord = localDbCtx
.DbSetPMTask
.Where(x => x.PMTaskId == dbResult.PMTaskId)
.FirstOrDefault();
if (parentRecord != null)
{
parentRecord.NumTaskDone = numDone;
}
// eseguo salvataggi!
localDbCtx.SaveChanges();
// segno fatto!
answ = true;
}
LogDebug("SchedMaintTaskSetDone", stopWatch.Elapsed);
stopWatch.Stop();
}
catch (Exception exc)
{
LogException("Eccezione durante SchedMaintTaskSetDone", exc);
}
}
await Task.Delay(1);
return answ;
}
/// <summary>
/// Inserisce un nuovo record status log calcolando durata da rec precedente..
/// </summary>
/// <param name="MachineId"></param>
/// <returns></returns>
public async Task<bool> StatusLogInsert(StatusLogModel newRec)
{
bool answ = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
TimeSpan ts;
bool needInsert = false;
// ultimo inserito...
var lastSet = localDbCtx
.DbSetStatusLog
.Where(x => x.MachineId == newRec.MachineId)
.OrderByDescending(x => x.EventLogId)
.Take(50)
.ToList();
var lastRec = lastSet
.OrderByDescending(x => x.DtRif)
.FirstOrDefault();
// se non trovato inserisco comunque
if (lastRec == null)
{
needInsert = true;
}
// se trovato controllo variazione...
else
{
// se cambia stato...
if (lastRec.CodStatus != newRec.CodStatus)
{
needInsert = true;
}
// oppure se è passato il giorno...
else if (lastRec.DtRif.Date < newRec.DtRif.Date)
{
needInsert = true;
}
// calcolo durata vecchio record...
lastRec.Duration = (float)DateTime.Now.Subtract(lastRec.DtRif).TotalMinutes;
}
if (needInsert)
{
localDbCtx
.DbSetStatusLog
.Add(newRec);
// salvo se ho NUOVO record
localDbCtx.SaveChanges();
LogDebug("StatusLogInsert", stopWatch.Elapsed);
stopWatch.Stop();
}
answ = true;
}
catch (Exception exc)
{
LogException("Eccezione durante StatusLogInsert", exc);
}
}
await Task.Delay(1);
return answ;
}
/// <summary>
/// Registrazione record esecuzione task x log
/// </summary>
/// <param name="recList"></param>
/// <returns></returns>
public bool TaskExecInsertLog(List<TaskExecModel> recList)
{
bool answ = false;
using (MapoMonoContext localDbCtx = new MapoMonoContext())
{
try
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
localDbCtx
.DbSetTaskExec
.AddRange(recList);
localDbCtx.SaveChanges();
answ = true;
stopWatch.Stop();
LogDebug("TaskExecInsertLog", stopWatch.Elapsed);
}
catch (Exception exc)
{
LogException("Eccezione durante TaskExecInsertLog", exc);
}
}
return answ;
}
#endregion Public Methods
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Limite durata eventi loggati x passare comunque a livello INFO...
/// </summary>
private int LogDurationLimitMs = 2000;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Accessor configurazione
/// </summary>
private IConfiguration appConf { get; set; } = null!;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Effettua Log tipo Debug
/// </summary>
/// <param name="message"></param>
/// <param name="ts"></param>
private void LogDebug(string message, TimeSpan ts)
{
// Se superato limite --> invio a INFO
if (ts.TotalMilliseconds > LogDurationLimitMs)
{
LogInfo($"{message} | DurationLimit Exceeded", ts);
}
else
{
Log.Debug($"{message} | {ts.TotalMilliseconds} ms");
}
}
/// <summary>
/// Effettua Log tipo ERROR
/// </summary>
/// <param name="message"></param>
/// <param name="exc">Eventuale exxeczione da traccaire</param>
private void LogError(string message)
{
Log.Error(message);
}
/// <summary>
/// Effettua Log tipo ERROR
/// </summary>
/// <param name="message"></param>
/// <param name="exc">Eventuale eccezione da tracciare</param>
private void LogException(string message, Exception exc)
{
Log.Error(exc, $"{message}{Environment.NewLine}{exc}");
}
/// <summary>
/// Effettua Log tipo TRACE
/// </summary>
/// <param name="message"></param>
/// <param name="ts"></param>
private void LogInfo(string message, TimeSpan ts)
{
Log.Info($"{message} | {ts.TotalMilliseconds} ms");
}
/// <summary>
/// Effettua Log tipo TRACE
/// </summary>
/// <param name="message"></param>
/// <param name="ts"></param>
private void LogTrace(string message, TimeSpan ts)
{
// Se superato limite --> invio a INFO
if (ts.TotalMilliseconds > LogDurationLimitMs)
{
LogInfo($"{message} | DurationLimit Exceeded", ts);
}
else
{
Log.Trace($"{message} | {ts.TotalMilliseconds} ms");
}
}
#endregion Private Methods
}
}