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
+3
View File
@@ -65,6 +65,9 @@
<None Update="logs\stdin.log">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="nssm.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
+58
View File
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue" />
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target xsi:type="File"
name="fileTarget"
fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} | ${uppercase:${level}} | ${logger:shortName=false} | ${message}"
archiveFileName="${basedir}/logs/${shortdate}.{###}.zip"
archiveNumbering="Sequence"
archiveAboveSize="10240000"
maxArchiveFiles="90"
enableArchiveFileCompression="true"
keepFileOpen="false"
/>
<target xsi:type="ColoredConsole"
name="consoleTarget"
layout="${longdate} | ${uppercase:${level}} | ${logger:shortName=true}| ${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
<!--<logger name="Microsoft.*" maxlevel="Info" final="true" />-->
<!--<logger name="*" minlevel="Trace" writeTo="consoleTarget" />-->
<logger name="*" minlevel="Info" writeTo="fileTarget" />
</rules>
</nlog>
+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}");
}
}
}