, MemoryTarget, (Flushing). :
public class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
try
{
logger.Log(LogLevel.Error, "Start of the process");
Business b = new Business();
b.DoSomethig();
logger.Debug("End of the Process.");
}
catch (Exception )
{
var target = (MemoryTarget)LogManager.Configuration.FindTargetByName("MemoTarget");
var logs = target.Logs;
foreach (string s in target.Logs)
{
Console.Write("logged: {0}", s);
}
}
}
}
nlog.config:
<targets>
<target xsi:type="Memory" name="MemoTarget" layout="${date:format=dd-MM-yyyy HH\:mm\:ss} | ${callsite} | ${message}" />
...
</targets>
...
<rules>
<logger name="*" minlevel="Debug" writeTo="MemoTarget" />
</rules>
....
:
logged: 30-10-2017 20:12:36 | NLog_Tests.Program.Main | Start of the process
logged: 30-10-2017 20:12:36 | NLog_Tests.Business.DoSomethig | Some business rule was executed
logged: 30-10-2017 20:12:36 | NLog_Tests.DAOClass.ExecuteCommand | some sql was executed
logged: 30-10-2017 20:12:36 | NLog_Tests.Program.Main | End of the Process.