Update SIM x usare NLog

This commit is contained in:
Samuele Locatelli
2022-03-11 16:24:06 +01:00
parent 418dfa778d
commit 4cdfd7cb8c
3 changed files with 105 additions and 15 deletions
+44 -15
View File
@@ -4,10 +4,12 @@ using MP.MONO.Core;
using MP.MONO.Core.CONF;
using Newtonsoft.Json;
using StackExchange.Redis;
using NLog;
string lineSep = "---------------------------------------------";
string redisConf = "nkcredis.steamware.net:6379,DefaultDatabase=7,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false,password=nkc.password";
Logger Log = LogManager.GetCurrentClassLogger();
Random rand = new Random();
List<MachineStatus>? statusList = new List<MachineStatus>();
List<MachineMode>? modeList = new List<MachineMode>();
@@ -17,12 +19,13 @@ DateTime lastLog = DateTime.Now.AddMinutes(-1);
bool verboseLog = false;
bool logWriting = false;
Console.WriteLine(lineSep);
Console.WriteLine($"Starting Machine SIM - redis server with param=[{redisConf}]");
Console.WriteLine(lineSep);
Console.WriteLine("");
Console.WriteLine("Running - press CTRL-C to stop SIM");
Console.WriteLine("");
logInfo(lineSep, true, true);
logInfo($"Starting Machine SIM", true, true);
logInfo($"Redis server param: {redisConf.Substring(0,20)}...", false, true);
logInfo(lineSep, true, true);
logInfo("", true, true);
logInfo("Running - press CTRL-C to stop SIM", false, true);
logInfo("", false, true);
//Create a connection
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisConf);
@@ -87,7 +90,34 @@ void setupConf()
}
}
/// <summary>
/// Effettua log INFO su file e se richiesto su console
/// </summary>
void logInfo(string msg, bool log2file = true, bool log2console = false)
{
if (log2console)
{
Console.WriteLine(msg);
}
if (log2file)
{
Log.Info(msg);
}
}
/// <summary>
/// Effettua log ERROR su file e se richiesto su console
/// </summary>
void logError(string msg, bool log2file = true, bool log2console = false)
{
if (log2console)
{
Console.WriteLine(msg);
}
if (log2file)
{
Log.Error(msg);
}
}
void saveAndSendMessage(string memKey, string value, string notifyChannel, string message)
{
@@ -99,7 +129,7 @@ void saveAndSendMessage(string memKey, string value, string notifyChannel, strin
sub.Publish(notifyChannel, message);
if (verboseLog)
{
Console.WriteLine($"[{notifyChannel}] key: {memKey} | val: {value} | message: {message}");
logInfo($"[{notifyChannel}] key: {memKey} | val: {value} | message: {message}");
}
else
{
@@ -121,25 +151,24 @@ void saveAndSendMessage(string memKey, string value, string notifyChannel, strin
if (adesso.Subtract(lastLog).TotalSeconds > 15)
{
lastLog = adesso;
Console.WriteLine(lineSep);
//Console.WriteLine($"{adesso:yyyy.MM.dd HH:mm:ss}");
//Console.WriteLine(lineSep);
logInfo(lineSep);
//logInfo($"{adesso:yyyy.MM.dd HH:mm:ss}");
//logInfo(lineSep);
// lavoro su copia...
var LogSimulatorCopy = new Dictionary<string, int>(LogSimulator);
foreach (var item in LogSimulatorCopy)
{
Console.WriteLine($"{item.Key, -20} {item.Value,20} rec");
logInfo($"{item.Key,-20} {item.Value,20} rec");
}
Console.WriteLine(lineSep);
Console.WriteLine("");
//logInfo(lineSep);
}
logWriting = false;
}
}
catch (Exception ex)
{
Console.WriteLine($"ERROR{Environment.NewLine}{ex}");
logError($"ERROR{Environment.NewLine}{ex}");
}
}
}