From e6f7e2fa8eca496ec05b0139f5a56126f04c2e95 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 13 Sep 2023 09:24:30 +0200 Subject: [PATCH] Modifica e bonifica memlayer --- SteamWare/memLayer.cs | 739 ++++++++++++++++++++++-------------------- 1 file changed, 390 insertions(+), 349 deletions(-) diff --git a/SteamWare/memLayer.cs b/SteamWare/memLayer.cs index 124263c..8b57558 100644 --- a/SteamWare/memLayer.cs +++ b/SteamWare/memLayer.cs @@ -1,5 +1,6 @@ using MongoDB.Driver; using Newtonsoft.Json; +using NLog; using StackExchange.Redis; using System; using System.Collections.Generic; @@ -15,52 +16,6 @@ namespace SteamWare /// public class memLayer { - #region Private Fields - - /// - /// Connessione lazy a redis... - /// - private Lazy lazyConnection = new Lazy(() => - { - string RedisConn = memLayer.ML.confReadString("RedisConn"); - if (string.IsNullOrEmpty(RedisConn)) - { - RedisConn = "localhost,abortConnect=false,ssl=false"; - } - - return ConnectionMultiplexer.Connect(RedisConn); - }); - - /// - /// Connessione lazy a redis... - /// - private Lazy lazyConnectionAdmin = new Lazy(() => - { - string RedisConnAdmin = memLayer.ML.confReadString("RedisConnAdmin"); - if (string.IsNullOrEmpty(RedisConnAdmin)) - { - RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true"; - } - - return ConnectionMultiplexer.Connect(RedisConnAdmin); - }); - - #endregion Private Fields - - #region Protected Fields - - /// - /// lettore file configurazione - /// - protected AppSettingsReader configAppSetReader; - - /// - /// Oggetto MongoDbCLient x accesso al motore - /// - protected MongoClient currMongoClient; - - #endregion Protected Fields - #region Public Fields /// @@ -85,23 +40,6 @@ namespace SteamWare #endregion Public Fields - #region Protected Constructors - - /// - /// classe gestione accessi a Session, cache, viewstate, configuration... - /// - protected memLayer() - { - // istanzia il conf setting reader... - configAppSetReader = new AppSettingsReader(); - // avvio e configuro TA - initTA(); - setupConnectionStringBase(); - setupMongo(); - } - - #endregion Protected Constructors - #region Public Enums /// @@ -132,59 +70,6 @@ namespace SteamWare #endregion Public Enums - #region Private Properties - - /// - /// Oggetto currentDb locale - /// - private IDatabase _currDB { get; set; } - - #endregion Private Properties - - #region Protected Properties - - /// - /// stringa conn x DB CONF - /// - protected string connStringDbConf - { - get - { - string answ = ""; - answ = confReadString("DbConfConnectionString"); - // se fosse vuota fallback a path dei permessi... - if (string.IsNullOrEmpty(answ)) - { - answ = confReadString("PermessiConnectionString"); - } - // se ancora vuota --> devAuth - if (string.IsNullOrEmpty(answ)) - { - answ = confReadString("DevicesAuthConnectionString"); - } - return answ; - } - } - - /// - /// Stringa di connessione mongoDb - /// - protected string mongoConnString - { - get - { - string answ = ""; - answ = confReadString("mdbConnString"); - if (string.IsNullOrEmpty(answ)) - { - answ = "mongodb://W2019-MONGODB:27017"; - } - return answ; - } - } - - #endregion Protected Properties - #region Public Properties /// @@ -302,7 +187,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in lettura valore maxAgeAppConf_min{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Error(string.Format("Errore in lettura valore maxAgeAppConf_min{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); } return maxAge * 60; } @@ -349,7 +234,8 @@ namespace SteamWare } /// - /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per impiego di sessioni avanzate come Redis) + /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per + /// impiego di sessioni avanzate come Redis) /// public bool serializeSession { @@ -378,7 +264,7 @@ namespace SteamWare catch (Exception exc) { answ = new Dictionary(); - logger.lg.scriviLog(string.Format("Errore in tabelleInCache{0}{1}", Environment.NewLine, exc)); + Log.Error(string.Format("Errore in tabelleInCache{0}{1}", Environment.NewLine, exc)); } return answ; } @@ -418,150 +304,6 @@ namespace SteamWare #endregion Public Properties - #region Private Methods - - /// - /// Effettua comaprazione x CHIAVE in KVP ASC - /// - /// - /// - /// - private int CompareKey(KeyValuePair x, KeyValuePair y) - { - return x.Key.CompareTo(y.Key); - } - - /// - /// Effettua comaprazione x CHIAVE in KVP DESC - /// - /// - /// - /// - private int CompareKeyDesc(KeyValuePair x, KeyValuePair y) - { - return y.Key.CompareTo(x.Key); - } - - /// - /// Init accesso MongoDb - /// - private void setupMongo() - { - currMongoClient = new MongoClient(mongoConnString); - } - - #endregion Private Methods - - #region Protected Methods - - /// - /// init dei table adapters - /// - protected void initTA() - { - taConfig = new DS_UtilityTableAdapters.ConfigTableAdapter(); - taConfigTmp = new DS_UtilityTableAdapters.ConfigTmpTableAdapter(); - } - - /// - /// Aggiornamento di una chiave in tabella config - /// - /// - /// - public void updateAppConf(string chiave, string valore) - { - // recupero valore attuale... - memLayer nML = new memLayer(); - nML.taConfig.updateQuery(chiave, valore); - resetAppConf(); - } - - /// - /// carica in ram oggetto AppConf - /// - /// - protected Dictionary ricaricaAppConf() - { - Dictionary answ = new Dictionary(); - // istanzio un NUOVO oggetto x evitare problemi init contestuali - memLayer nML = new memLayer(); - var tabDati = nML.taConfig.GetData(); - // carico - foreach (DS_Utility.ConfigRow riga in tabDati) - { - try - { - answ.Add(riga.chiave, riga.valore); - } - catch (Exception exc) - { - logger.lg.scriviLog(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc), tipoLog.EXCEPTION); - } - } - // log ricarica - Logger.Logging.Instance.Info($"Effettuata procedura ricaricaAppConf per {answ.Count} records"); - return answ; - } - - /// - /// effettua setup dei connection strings da web.config delal singola applicazione - /// - protected virtual void setupConnectionStringBase() - { - // connections del db - taConfig.Connection.ConnectionString = connStringDbConf; - taConfigTmp.Connection.ConnectionString = connStringDbConf; - } - - /// - /// avvio oggetto AppConf in ram - /// - protected void startupAppConf() - { - // SOLO SE ho la chiave x abilitare config su DB... - if (confReadString("DbConfConnectionString") != "") - { - try - { - if (redKeyPresent(ACBH)) - { - AppConf = new Dictionary(); - foreach (var item in redGetHash(ACBH)) - { - if (AppConf.ContainsKey(item.Key)) - { - AppConf[item.Key] = item.Value; - } - else - { - AppConf.Add(item.Key, item.Value); - } - } - } - else - { - AppConf = ricaricaAppConf(); - KeyValuePair[] valori = new KeyValuePair[AppConf.Count]; - int i = 0; - foreach (var item in AppConf) - { - valori[i] = new KeyValuePair(item.Key, item.Value); - i++; - } - // salvo in redis valori (con TTL) - redSaveHash(ACBH, valori, maxAgeAppConf); - Logger.Logging.Instance.Info("Completato procedura startupAppConf"); - } - } - catch (Exception exc) - { - Logger.Logging.Instance.Error($"Errore in startupAppConf:{Environment.NewLine}{exc}"); - } - } - } - - #endregion Protected Methods - #region Public Methods /// @@ -575,12 +317,13 @@ namespace SteamWare } /// - /// aggiunge la stringa corrente nel dictionary delle tabelle messe in cache e da aggiornare su comando update + /// aggiunge la stringa corrente nel dictionary delle tabelle messe in cache e da aggiornare + /// su comando update /// /// public void addTabInCache(string nuovaTab) { - // provo ad aggiungere nuova tab in elenco... + // provo ad aggiungere nuova tab in elenco... Dictionary _tabelleInCache = tabelleInCache; try { @@ -592,13 +335,14 @@ namespace SteamWare } /// - /// aggiunge la stringa corrente nel dictionary delle tabelle messe in session che vanno preservate da comando update (es: oggetto selezionato...) + /// aggiunge la stringa corrente nel dictionary delle tabelle messe in session che vanno + /// preservate da comando update (es: oggetto selezionato...) /// /// /// public void addValInSession(string nomePar, string valore) { - // provo ad aggiungere nuova tab in elenco... + // provo ad aggiungere nuova tab in elenco... Dictionary _valoriInSession2Survive = valSess2SurvUpd; // verifico se fare update o insert... if (_valoriInSession2Survive.ContainsKey(nomePar)) @@ -693,12 +437,12 @@ namespace SteamWare bool fatto = bool.TryParse(sVal, out answ); if (!fatto) { - logger.lg.scriviLog($"Errore in lettura chiave [{chiave}] durante cdvb: ricevuto {sVal}", tipoLog.EXCEPTION); + Log.Info($"Errore in lettura chiave [{chiave}] durante cdvb: ricevuto {sVal}", tipoLog.EXCEPTION); } } catch (Exception exc) { - logger.lg.scriviLog($"Errore in lettura chiave [{chiave}] durante cdvb{Environment.NewLine}{exc}", tipoLog.EXCEPTION); + Log.Error($"Errore in lettura chiave [{chiave}] durante cdvb{Environment.NewLine}{exc}", tipoLog.EXCEPTION); } } return answ; @@ -721,12 +465,12 @@ namespace SteamWare bool fatto = int.TryParse(sVal, out answ); if (!fatto) { - logger.lg.scriviLog($"Errore in lettura chiave [{chiave}] durante cdvi: ricevuto {sVal}", tipoLog.EXCEPTION); + Log.Info($"Errore in lettura chiave [{chiave}] durante cdvi: ricevuto {sVal}", tipoLog.EXCEPTION); } } catch (Exception exc) { - logger.lg.scriviLog($"Errore in lettura chiave [{chiave}] durante cdvi{Environment.NewLine}{exc}", tipoLog.EXCEPTION); + Log.Error($"Errore in lettura chiave [{chiave}] durante cdvi{Environment.NewLine}{exc}", tipoLog.EXCEPTION); } } return answ; @@ -779,7 +523,7 @@ namespace SteamWare // controllo SE in redis ci sia ancora il valore, altrimenti rileggo... if (!redKeyPresent(ACBH)) { - logger.lg.scriviLog(string.Format("Manca HASH in redis --> rileggo da DB --> REDIS --> Memoria | {0} | {1}", ACBH, chiave), tipoLog.INFO); + Log.Info($"Manca HASH in redis --> rileggo da DB --> REDIS --> Memoria | {ACBH} | {chiave}"); resetAppConf(); } // provo a leggere da DICT @@ -791,7 +535,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in lettura AppConf[{0}] durante configDbVal{1}{2}", chiave, Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Error($"Errore in lettura AppConf[{chiave}] durante configDbVal{Environment.NewLine}{exc}"); } } } @@ -1138,7 +882,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Error($"Errore in getRSV:{Environment.NewLine}{exc}"); } return answInt; } @@ -1179,7 +923,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in getRKeys:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Error($"Errore in getRKeys:{Environment.NewLine}{exc}"); } return answ; } @@ -1198,7 +942,7 @@ namespace SteamWare } catch (Exception exc) { - //logger.lg.scriviLog(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Error($"Errore in getRSV:{Environment.NewLine}{exc}"); } return answ; } @@ -1217,8 +961,10 @@ namespace SteamWare { answ = HttpContext.Current.Request.Cookies[nome].Value != ""; } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in hasCookieVal:{Environment.NewLine}{exc}"); + } } return answ; } @@ -1284,7 +1030,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in verifica isInCacheObject REDIS per chiave{0}{1}{2}", nomeVar, Environment.NewLine, exc)); + Log.Error($"Errore in verifica isInCacheObject REDIS per chiave{nomeVar}{Environment.NewLine}{exc}"); } } else @@ -1294,8 +1040,10 @@ namespace SteamWare { stringAnsw = HttpContext.Current.Cache[nomeVar].ToString() != ""; } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in verifica HttpContext.Current.Cache per chiave{nomeVar}{Environment.NewLine}{exc}"); + } // infine condizione doppia... try { @@ -1303,7 +1051,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in verifica isInCacheObject per chiave{0}{1}{2}", nomeVar, Environment.NewLine, exc)); + Log.Error($"Errore in verifica NULL per HttpContext.Current.Cache per chiave{nomeVar}{Environment.NewLine}{exc}"); } } return answ; @@ -1317,22 +1065,15 @@ namespace SteamWare public bool isInSessionObject(string nomeVar) { bool answ = false; - bool stringAnsw = false; // cerco se ci sia... try { - //stringAnsw = (string)HttpContext.Current.Session[nomeVar].ToString() != ""; - stringAnsw = HttpContext.Current.Session[nomeVar] != null; + answ = (HttpContext.Current.Session[nomeVar] != null); } - catch - { } - // infine condizione doppia... - try + catch (Exception exc) { - answ = (HttpContext.Current.Session[nomeVar] != null && stringAnsw); + Log.Error($"Errore in isInSessionObject:{Environment.NewLine}{exc}"); } - catch - { } return answ; } @@ -1409,8 +1150,10 @@ namespace SteamWare { answ = Convert.ToBoolean(HttpContext.Current.Request.QueryString[nome]); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in QSB:{Environment.NewLine}{exc}"); + } return answ; } @@ -1426,8 +1169,10 @@ namespace SteamWare { answ = Convert.ToDateTime(HttpContext.Current.Request.QueryString[nome]); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in QSD:{Environment.NewLine}{exc}"); + } return answ; } @@ -1443,8 +1188,10 @@ namespace SteamWare { answ = Convert.ToInt32(HttpContext.Current.Request.QueryString[nome]); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in QSI:{Environment.NewLine}{exc}"); + } return answ; } @@ -1462,8 +1209,10 @@ namespace SteamWare { answ = HttpContext.Current.Request.QueryString[nome].ToString().Trim(); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in QSS:{Environment.NewLine}{exc}"); + } } return answ; } @@ -1476,8 +1225,7 @@ namespace SteamWare public int redCountKey(string keyPattern) { int answ = 0; - // cerco se ci sia valore in redis... - // se vuoto = ALL... + // cerco se ci sia valore in redis... se vuoto = ALL... keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; try { @@ -1492,7 +1240,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + Log.Error($"Errore in redCountKey:{Environment.NewLine}{exc}"); } return answ; } @@ -1512,8 +1260,10 @@ namespace SteamWare cache.KeyDelete(chiave); answ = true; } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redDelKey:{Environment.NewLine}{exc}"); + } return answ; } @@ -1525,8 +1275,7 @@ namespace SteamWare public bool redFlushKey(string keyPattern) { bool answ = false; - // cerco se ci sia valore in redis... - // se vuoto = ALL... + // cerco se ci sia valore in redis... se vuoto = ALL... keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; try { @@ -1543,7 +1292,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + Log.Error($"Errore in redFlushKey:{Environment.NewLine}{exc}"); } return answ; } @@ -1578,7 +1327,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + Log.Error($"Errore in redGetCounterByKey 01{Environment.NewLine}{exc}"); } // ora recupero valori! var valori = getRKeys(chiavi); @@ -1592,8 +1341,10 @@ namespace SteamWare answ.Add(new KeyValuePair(chiavi[i], currVal)); } } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redGetCounterByKey 02{Environment.NewLine}{exc}"); + } // se richiesto riordino... switch (orderBy) { @@ -1640,8 +1391,10 @@ namespace SteamWare i++; } } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redGetHash{Environment.NewLine}{exc}"); + } return answ; } @@ -1663,8 +1416,10 @@ namespace SteamWare answ.Add(item.Name, item.Value); } } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redGetHashDict{Environment.NewLine}{exc}"); + } return answ; } @@ -1685,8 +1440,10 @@ namespace SteamWare RedisValue valOut = cache.HashGet(chiave, campo); answ = valOut.ToString(); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redGetHashField{Environment.NewLine}{exc}"); + } return answ; } @@ -1698,8 +1455,7 @@ namespace SteamWare public List redGetKeys(string keyPattern) { List answ = new List(); - // cerco se ci sia valore in redis... - // se vuoto = ALL... + // cerco se ci sia valore in redis... se vuoto = ALL... keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; try { @@ -1714,13 +1470,14 @@ namespace SteamWare } catch (Exception exc) { - Logger.Logging.LogException(exc, Logger.Constants.ERROR_LEVEL.Error); + Log.Error($"Errore in redGetKeys{Environment.NewLine}{exc}"); } return answ; } /// - /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto... + /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da + /// funzionalita' DbConfig) + keyName richiesto... /// public string redHash(string keyName) { @@ -1729,8 +1486,10 @@ namespace SteamWare { answ = string.Format("{0}:{1}:{2}:{3}", confReadString("CodModulo"), taConfig.Connection.DataSource, taConfig.Connection.Database, keyName).Replace("\\", "_"); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redHash{Environment.NewLine}{exc}"); + } return answ; } @@ -1749,7 +1508,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in redHashPresent per la key {2}{0}{1}", Environment.NewLine, exc, key), tipoLog.EXCEPTION); + Log.Error($"Errore in redHashPresent | {key}{Environment.NewLine}{exc}"); } return answ; } @@ -1767,8 +1526,10 @@ namespace SteamWare RedisKey chiave = key; answ = redHashPresent(chiave); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redHashPresentSz | {key}{Environment.NewLine}{exc}"); + } return answ; } @@ -1787,7 +1548,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in redKeyPresent per la key {2}:{0}{1}", Environment.NewLine, exc, key), tipoLog.EXCEPTION); + Log.Error($"Errore in redKeyPresent | RedisKey: {key}{Environment.NewLine}{exc}"); } return answ; } @@ -1805,8 +1566,10 @@ namespace SteamWare RedisKey chiave = key; answ = redKeyPresent(chiave); } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redKeyPresentSz | {key}{Environment.NewLine}{exc}"); + } return answ; } @@ -1833,8 +1596,10 @@ namespace SteamWare cache.HashSet(chiave, valori); answ = true; } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redSaveHash{Environment.NewLine}{exc}"); + } return answ; } @@ -1843,7 +1608,9 @@ namespace SteamWare /// /// chiave /// valori - /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// + /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// /// public bool redSaveHash(string hashKey, KeyValuePair[] hashFields, double expireSeconds = -1) { @@ -1859,8 +1626,10 @@ namespace SteamWare } //answ = true; } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redSaveHash | {hashKey}{Environment.NewLine}{exc}"); + } return answ; } @@ -1887,8 +1656,10 @@ namespace SteamWare cache.HashSet(chiave, valori); answ = true; } - catch - { } + catch (Exception exc) + { + Log.Error($"Errore in redSaveHashDict | {hashKey}{Environment.NewLine}{exc}"); + } return answ; } @@ -1897,7 +1668,9 @@ namespace SteamWare /// /// chiave /// valori - /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// + /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// /// public bool redSaveHashDict(string hashKey, Dictionary hashFields, double expireSeconds = -1) { @@ -1970,7 +1743,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + Log.Info(string.Format("{0}", exc), tipoLog.EXCEPTION); } return answ; } @@ -1998,7 +1771,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in resetRCnt:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Info(string.Format("Errore in resetRCnt:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); } return answ; } @@ -2121,7 +1894,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in setRCD:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Info(string.Format("Errore in setRCD:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); } return answ; } @@ -2140,13 +1913,13 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in setRCI:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Info(string.Format("Errore in setRCI:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); } return answ; } /// - /// Salva un set KVP (Key Value Pair) in RedisCache + /// Salva un set KVP (Key Value Pair) in RedisCache /// /// Set KVP chiave-valore da salvare /// @@ -2160,7 +1933,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format("Errore in setRKeys:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + Log.Info(string.Format("Errore in setRKeys:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); } return answ; } @@ -2183,12 +1956,12 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog($"Eccezzione in setRSV | chiave: {chiave} | valore: {valore}{Environment.NewLine}{exc}", tipoLog.EXCEPTION); + Log.Info($"Eccezzione in setRSV | chiave: {chiave} | valore: {valore}{Environment.NewLine}{exc}", tipoLog.EXCEPTION); } } else { - logger.lg.scriviLog("Errore: chiave non valida (vuota) in setRSV", tipoLog.ERROR); + Log.Info("Errore: chiave non valida (vuota) in setRSV", tipoLog.ERROR); } return answ; } @@ -2212,7 +1985,7 @@ namespace SteamWare } catch (Exception exc) { - logger.lg.scriviLog(string.Format($"Errore in setRSV | chiave: {chiave} | valore: {valore} | TTL: {TTL_sec}{Environment.NewLine}{exc}"), tipoLog.EXCEPTION); + Log.Info(string.Format($"Errore in setRSV | chiave: {chiave} | valore: {valore} | TTL: {TTL_sec}{Environment.NewLine}{exc}"), tipoLog.EXCEPTION); } return answ; } @@ -2240,7 +2013,9 @@ namespace SteamWare /// /// nome della variabile /// DataSet da salvare - /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// + /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// /// public bool setSessionDataSet(string nome, DataSet dSet, bool surviveUpdate) { @@ -2280,7 +2055,9 @@ namespace SteamWare /// /// nome della variabile /// DataTable da salvare - /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// + /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// public bool setSessionDataTable(string nome, DataTable dTable, bool surviveUpdate) { bool _done = false; @@ -2319,7 +2096,9 @@ namespace SteamWare /// /// nome della variabile /// valore associato - /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// + /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// /// public bool setSessionVal(string nome, object valore, bool surviveUpdate) { @@ -2385,6 +2164,268 @@ namespace SteamWare return answ; } + /// + /// Aggiornamento di una chiave in tabella config + /// + /// + /// + public void updateAppConf(string chiave, string valore) + { + // recupero valore attuale... + memLayer nML = new memLayer(); + nML.taConfig.updateQuery(chiave, valore); + resetAppConf(); + } + #endregion Public Methods + + #region Protected Fields + + /// + /// lettore file configurazione + /// + protected AppSettingsReader configAppSetReader; + + /// + /// Oggetto MongoDbCLient x accesso al motore + /// + protected MongoClient currMongoClient; + + #endregion Protected Fields + + #region Protected Constructors + + /// + /// classe gestione accessi a Session, cache, viewstate, configuration... + /// + protected memLayer() + { + // istanzia il conf setting reader... + configAppSetReader = new AppSettingsReader(); + // avvio e configuro TA + initTA(); + setupConnectionStringBase(); + setupMongo(); + } + + #endregion Protected Constructors + + #region Protected Properties + + /// + /// stringa conn x DB CONF + /// + protected string connStringDbConf + { + get + { + string answ = ""; + answ = confReadString("DbConfConnectionString"); + // se fosse vuota fallback a path dei permessi... + if (string.IsNullOrEmpty(answ)) + { + answ = confReadString("PermessiConnectionString"); + } + // se ancora vuota --> devAuth + if (string.IsNullOrEmpty(answ)) + { + answ = confReadString("DevicesAuthConnectionString"); + } + return answ; + } + } + + /// + /// Stringa di connessione mongoDb + /// + protected string mongoConnString + { + get + { + string answ = ""; + answ = confReadString("mdbConnString"); + if (string.IsNullOrEmpty(answ)) + { + answ = "mongodb://W2019-MONGODB:27017"; + } + return answ; + } + } + + #endregion Protected Properties + + #region Protected Methods + + /// + /// init dei table adapters + /// + protected void initTA() + { + taConfig = new DS_UtilityTableAdapters.ConfigTableAdapter(); + taConfigTmp = new DS_UtilityTableAdapters.ConfigTmpTableAdapter(); + } + + /// + /// carica in ram oggetto AppConf + /// + /// + protected Dictionary ricaricaAppConf() + { + Dictionary answ = new Dictionary(); + // istanzio un NUOVO oggetto x evitare problemi init contestuali + memLayer nML = new memLayer(); + var tabDati = nML.taConfig.GetData(); + // carico + foreach (DS_Utility.ConfigRow riga in tabDati) + { + try + { + answ.Add(riga.chiave, riga.valore); + } + catch (Exception exc) + { + Log.Error($"Errore procedura ricaricaAppConf per kvp: {riga.chiave} / {riga.valore}{Environment.NewLine}{exc}"); + } + } + // log ricarica + Logger.Logging.Instance.Info($"Effettuata procedura ricaricaAppConf per {answ.Count} records"); + return answ; + } + + /// + /// effettua setup dei connection strings da web.config delal singola applicazione + /// + protected virtual void setupConnectionStringBase() + { + // connections del db + taConfig.Connection.ConnectionString = connStringDbConf; + taConfigTmp.Connection.ConnectionString = connStringDbConf; + } + + /// + /// avvio oggetto AppConf in ram + /// + protected void startupAppConf() + { + // SOLO SE ho la chiave x abilitare config su DB... + if (confReadString("DbConfConnectionString") != "") + { + try + { + if (redKeyPresent(ACBH)) + { + AppConf = new Dictionary(); + foreach (var item in redGetHash(ACBH)) + { + if (AppConf.ContainsKey(item.Key)) + { + AppConf[item.Key] = item.Value; + } + else + { + AppConf.Add(item.Key, item.Value); + } + } + } + else + { + AppConf = ricaricaAppConf(); + KeyValuePair[] valori = new KeyValuePair[AppConf.Count]; + int i = 0; + foreach (var item in AppConf) + { + valori[i] = new KeyValuePair(item.Key, item.Value); + i++; + } + // salvo in redis valori (con TTL) + redSaveHash(ACBH, valori, maxAgeAppConf); + Logger.Logging.Instance.Info("Completato procedura startupAppConf"); + } + } + catch (Exception exc) + { + Logger.Logging.Instance.Error($"Errore in startupAppConf:{Environment.NewLine}{exc}"); + } + } + } + + #endregion Protected Methods + + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + /// + /// Connessione lazy a redis... + /// + private Lazy lazyConnection = new Lazy(() => + { + string RedisConn = memLayer.ML.confReadString("RedisConn"); + if (string.IsNullOrEmpty(RedisConn)) + { + RedisConn = "localhost,abortConnect=false,ssl=false"; + } + + return ConnectionMultiplexer.Connect(RedisConn); + }); + + /// + /// Connessione lazy a redis... + /// + private Lazy lazyConnectionAdmin = new Lazy(() => + { + string RedisConnAdmin = memLayer.ML.confReadString("RedisConnAdmin"); + if (string.IsNullOrEmpty(RedisConnAdmin)) + { + RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true"; + } + + return ConnectionMultiplexer.Connect(RedisConnAdmin); + }); + + #endregion Private Fields + + #region Private Properties + + /// + /// Oggetto currentDb locale + /// + private IDatabase _currDB { get; set; } + + #endregion Private Properties + + #region Private Methods + + /// + /// Effettua comaprazione x CHIAVE in KVP ASC + /// + /// + /// + /// + private int CompareKey(KeyValuePair x, KeyValuePair y) + { + return x.Key.CompareTo(y.Key); + } + + /// + /// Effettua comaprazione x CHIAVE in KVP DESC + /// + /// + /// + /// + private int CompareKeyDesc(KeyValuePair x, KeyValuePair y) + { + return y.Key.CompareTo(x.Key); + } + + /// + /// Init accesso MongoDb + /// + private void setupMongo() + { + currMongoClient = new MongoClient(mongoConnString); + } + + #endregion Private Methods } } \ No newline at end of file