198 lines
5.9 KiB
C#
198 lines
5.9 KiB
C#
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Net;
|
|
|
|
namespace IOB_MAN
|
|
{
|
|
public class utils
|
|
{
|
|
#region logging
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
public static Logger lg = LogManager.GetCurrentClassLogger();
|
|
/// <summary>
|
|
/// Effettua logging INFO corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="txt2log"></param>
|
|
public static void lgInfo(string txt2log)
|
|
{
|
|
lg.Factory.Configuration.Variables["codApp"] = "IOB-MAN";
|
|
lg.Info(txt2log);
|
|
}
|
|
/// <summary>
|
|
/// Effettua logging ERROR corretto impostanto anche la variabile IOB prima di scrivere...
|
|
/// </summary>
|
|
/// <param name="txt2log"></param>
|
|
public static void lgError(string txt2log)
|
|
{
|
|
lg.Factory.Configuration.Variables["codApp"] = "IOB-MAN";
|
|
lg.Error(txt2log);
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Helper formattazione durata human readable
|
|
/// </summary>
|
|
/// <param name="timeSpan"></param>
|
|
/// <param name="textMode">textMode = come linguaggio scritto</param>
|
|
/// <returns></returns>
|
|
public static string FormatTimeSpan(TimeSpan timeSpan, bool textMode = false)
|
|
{
|
|
if (textMode)
|
|
{
|
|
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}";
|
|
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1}{t.Item2}";
|
|
Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {(t.Item1 == 1 ? t.Item2 : t.Item2.Replace("giorno", "giorni").Replace("ora", "ore"))}";
|
|
var components = new List<Tuple<int, string>>
|
|
{
|
|
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<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}";
|
|
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1}{t.Item2}";
|
|
Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1:00}";
|
|
var components = new List<Tuple<int, string>>
|
|
{
|
|
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 gestione IP/Hostname
|
|
|
|
public static string machineName
|
|
{
|
|
get
|
|
{
|
|
return Dns.GetHostName();
|
|
}
|
|
}
|
|
|
|
public static List<string> machineIp
|
|
{
|
|
get
|
|
{
|
|
List<string> ipList = new List<string>();
|
|
try
|
|
{
|
|
IPAddress[] ipAddresses = Dns.GetHostAddresses(machineName);
|
|
foreach (var ip in ipAddresses)
|
|
{
|
|
ipList.Add($"{ip}");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
// Machine not found...
|
|
lgError($"Eccezione in recupero MachineIp{Environment.NewLine}{exc}");
|
|
}
|
|
return ipList;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region utils lettura app.config
|
|
|
|
/// <summary>
|
|
/// legge conf in formato char
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static char CRC(string key)
|
|
{
|
|
char answ = '-';
|
|
try
|
|
{
|
|
answ = ConfigurationManager.AppSettings[key].ToCharArray()[0];
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// legge conf in formato stringa
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static string CRS(string key)
|
|
{
|
|
string answ = "";
|
|
try
|
|
{
|
|
answ = ConfigurationManager.AppSettings[key].ToString();
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// legge conf in formato INT
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static Int32 CRI(string key)
|
|
{
|
|
int answ = 0;
|
|
try
|
|
{
|
|
answ = Convert.ToInt32(CRS(key));
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// legge conf in formato BOOLean
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static bool CRB(string key)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = Convert.ToBoolean(CRS(key));
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|