From 4cdfd7cb8cfe3beaf6751b64db405e44b4581a46 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 11 Mar 2022 16:24:06 +0100 Subject: [PATCH] Update SIM x usare NLog --- MP.MONO.SIM/MP.MONO.SIM.csproj | 3 ++ MP.MONO.SIM/NLog.config | 58 +++++++++++++++++++++++++++++++++ MP.MONO.SIM/Program.cs | 59 +++++++++++++++++++++++++--------- 3 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 MP.MONO.SIM/NLog.config diff --git a/MP.MONO.SIM/MP.MONO.SIM.csproj b/MP.MONO.SIM/MP.MONO.SIM.csproj index 18d37a4..1c7c06b 100644 --- a/MP.MONO.SIM/MP.MONO.SIM.csproj +++ b/MP.MONO.SIM/MP.MONO.SIM.csproj @@ -65,6 +65,9 @@ Always + + Always + Always diff --git a/MP.MONO.SIM/NLog.config b/MP.MONO.SIM/NLog.config new file mode 100644 index 0000000..580738d --- /dev/null +++ b/MP.MONO.SIM/NLog.config @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MP.MONO.SIM/Program.cs b/MP.MONO.SIM/Program.cs index 9a45059..6b570c4 100644 --- a/MP.MONO.SIM/Program.cs +++ b/MP.MONO.SIM/Program.cs @@ -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? statusList = new List(); List? modeList = new List(); @@ -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() } } - +/// +/// Effettua log INFO su file e se richiesto su console +/// +void logInfo(string msg, bool log2file = true, bool log2console = false) +{ + if (log2console) + { + Console.WriteLine(msg); + } + if (log2file) + { + Log.Info(msg); + } +} +/// +/// Effettua log ERROR su file e se richiesto su console +/// +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(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}"); } } }