I am using NLog with Common Logging Please find my complete code snippets
I have mentioned the current output and expected output too I want separate memory logger per thread, they should not interact with each other.
Config
<targets>
<target name="LogFile"
xsi:type="File"
fileName="${mdc:item=LogsDirectory}\logfile.txt"
layout="${message}"
maxArchiveFiles="10"
archiveAboveSize="20000000"
archiveNumbering="Sequence"
archiveFileName="${mdc:item=LogsDirectory}\logfile.{#}.txt"/>
<target name="Memory" xsi:type="Memory" layout="${message}"/>
</targets>`
<rules>
<!-- add your logging rules here -->
<logger name="LogFile" minlevel="Debug" writeTo="LogFile" />
<logger name="LogFile" minlevel="Info" writeTo="Memory" />
</rules>
Code Snippet using config:
static void Main(string[] args)
{
List<Task> tasks = new List<Task>();
for (int ctr = 1; ctr <= 3; ctr++)
{
tasks.Add(Task.Factory.StartNew(FirstWorldTask));
}
Task.WaitAll(tasks.ToArray());
}
private static void FirstWorldTask()
{
var folderName = rnd.Next();
var scenarioId = rnd.Next().ToString();
Console.WriteLine(scenarioId);
NLog.MappedDiagnosticsContext.Set("LogsDirectory", folderName);
ILog logger = LogManager.GetLogger("LogFile");
var memoryTarget = (MemoryTarget)NLog.LogManager.Configuration.FindTargetByName("Memory");
memoryTarget.Logs.Clear();
for (int i = 0; i < 5; i++)
{
logger.Info(i + " " + scenarioId);
Thread.Sleep(10);
}
for (int i = 0; i < memoryTarget.Logs.Count; i++)
{
Console.WriteLine(memoryTarget.Logs[i] + " " + scenarioId);
}
memoryTarget.Logs.Clear();
}
Random Numbers
- 960258078
- 879841817
- 1123570211
Current Output
- 1 960258078 960258078
- 0 1123570211 960258078
- 1 879841817 960258078
- 2 960258078 960258078
- 1 1123570211 960258078
- 2 879841817 960258078
- 3 960258078 960258078
- 2 1123570211 960258078
- 3 879841817 960258078
- 4 960258078 960258078
- 3 1123570211 960258078
- 4 879841817 960258078
- 4 1123570211 1123570211
Expected Output
- 0 960258078 960258078
- 1 960258078 960258078
- 2 960258078 960258078
- 3 960258078 960258078
- 4 960258078 960258078
- 0 879841817 879841817
- 1 879841817 879841817
- 2 879841817 879841817
- 3 879841817 879841817
- 4 879841817 879841817
- 0 1123570211 1123570211
- 1 1123570211 1123570211
- 2 1123570211 1123570211
- 3 1123570211 1123570211
- 4 1123570211 1123570211
Code Snippet using Programmatically
private static void FirstWorldTask()
{
var folderName = rnd.Next();
var scenarioId = rnd.Next().ToString();
Console.WriteLine(scenarioId);
NLog.MappedDiagnosticsContext.Set("LogsDirectory", folderName);
ILog logger = LogManager.GetLogger("LogFile");
var memoryTarget = new MemoryTarget();
memoryTarget.Name = "Memory_" + scenarioId;
memoryTarget.Layout = "${message}";
var config = NLog.LogManager.Configuration;
config.AddTarget(memoryTarget.Name, memoryTarget);
var rule = new LoggingRule("LogFile", LogLevel.Info, memoryTarget);
config.LoggingRules.Add(rule);
NLog.LogManager.Configuration = config;
for (int i = 0; i < 5; i++)
{
logger.Info(i + " " + scenarioId);
Thread.Sleep(10);
}
while (memoryTarget.Logs.Count > 0)
{
int i = 0;
while (memoryTarget.Logs.Count > 0 && i < 5)
{
Console.WriteLine(memoryTarget.Logs.First() + " " + scenarioId);
memoryTarget.Logs.RemoveAt(0);
i++;
}
}
}
Random Numbers
- 903471573
- 560466050
- 898036495
Programmed Output
- 0 560466050 560466050
- 1 560466050 560466050
- 0 898036495 560466050
- 0 903471573 560466050
- 1 898036495 560466050
- 1 903471573 560466050
- 2 560466050 560466050
- 2 903471573 560466050
- 3 560466050 560466050
- 2 898036495 560466050
- 3 903471573 560466050
- 3 898036495 560466050
- 4 560466050 560466050
- 4 903471573 560466050
- 0 560466050 903471573
- 1 560466050 903471573
- 0 898036495 903471573
- 0 903471573 903471573
- 1 898036495 903471573
- 1 903471573 903471573
- 2 560466050 903471573
- 2 903471573 903471573
- 3 560466050 903471573
- 2 898036495 903471573
- 3 903471573 903471573
- 3 898036495 903471573
- 4 560466050 903471573
- 4 903471573 903471573
- 0 560466050 898036495
- 1 560466050 898036495
- 0 898036495 898036495
- 0 903471573 898036495
- 1 898036495 898036495
- 1 903471573 898036495
- 2 560466050 898036495
- 2 903471573 898036495
- 3 560466050 898036495
- 2 898036495 898036495
- 3 903471573 898036495
- 3 898036495 898036495
- 4 560466050 898036495
- 4 903471573 898036495
- 4 898036495 898036495
Logs are getting written on all the memoryTargets, instead of only one