using NLog; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; namespace IOB_MAN { public class utils { #region logging /// /// Classe logger /// public static Logger lg = LogManager.GetCurrentClassLogger(); /// /// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere... /// /// public static void lgInfo(string txt2log) { lg.Factory.Configuration.Variables["codApp"] = "IOB-MAN"; lg.Info(txt2log); } /// /// Effettua logging ERROR corretto impostanto anche la variabile IOB prima di scrivere... /// /// public static void lgError(string txt2log) { lg.Factory.Configuration.Variables["codApp"] = "IOB-MAN"; lg.Error(txt2log); } #endregion /// /// Helper formattazione durata human readable /// /// /// textMode = come linguaggio scritto /// public static string FormatTimeSpan(TimeSpan timeSpan, bool textMode=false) { if (textMode) { //Func, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}"; //Func, string> tupleFormatter = t => $"{t.Item1}{t.Item2}"; Func, string> tupleFormatter = t => $"{t.Item1} {(t.Item1 == 1 ? t.Item2 : t.Item2.Replace("giorno", "giorni").Replace("ora", "ore"))}"; var components = new List> { Tuple.Create((int) timeSpan.TotalDays, "giorno"), Tuple.Create(timeSpan.Hours, "ora"), Tuple.Create(timeSpan.Minutes, "min"), Tuple.Create(timeSpan.Seconds, "sec"), }; components.RemoveAll(i => i.Item1 == 0); string extra = ""; if (components.Count > 1) { var finalComponent = components[components.Count - 1]; components.RemoveAt(components.Count - 1); extra = $" e {tupleFormatter(finalComponent)}"; } return $"{string.Join(", ", components.Select(tupleFormatter))}{extra}"; } else { //Func, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}"; //Func, string> tupleFormatter = t => $"{t.Item1}{t.Item2}"; Func, string> tupleFormatter = t => $"{t.Item1:00}"; var components = new List> { Tuple.Create((int) timeSpan.TotalDays, "g"), Tuple.Create(timeSpan.Hours, ""), Tuple.Create(timeSpan.Minutes, ""), Tuple.Create(timeSpan.Seconds, ""), }; //components.RemoveAll(i => i.Item1 == 0); return $"{string.Join(":", components.Select(tupleFormatter))}"; } } #region utils lettura app.config /// /// legge conf in formato char /// /// /// public static char CRC(string key) { char answ = '-'; try { answ = ConfigurationManager.AppSettings[key].ToCharArray()[0]; } catch { } return answ; } /// /// legge conf in formato stringa /// /// /// public static string CRS(string key) { string answ = ""; try { answ = ConfigurationManager.AppSettings[key].ToString(); } catch { } return answ; } /// /// legge conf in formato INT /// /// /// public static Int32 CRI(string key) { int answ = 0; try { answ = Convert.ToInt32(CRS(key)); } catch { } return answ; } /// /// legge conf in formato BOOLean /// /// /// public static bool CRB(string key) { bool answ = false; try { answ = Convert.ToBoolean(CRS(key)); } catch { } return answ; } #endregion } }