假设我的nlog.config中有以下内容(取自http://nlog-project.org/documentation/v2.0.1/html/T_NLog_Targets_MemoryTarget.htm):
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="memory" xsi:type="Memory" layout="${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="memory" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
如何以编程方式访问此目标?我试图在文本框中显示日志.
这里完全相同的问题,这对我有用:
var target =(MemoryTarget)LogManager.Configuration.FindTargetByName("memory");
var log = string.Join("\r\n", target.Logs);
txtLog.Text = log;
Run Code Online (Sandbox Code Playgroud)
您可以使用 LoggingConfiguration.FindTargetByName 传入目标名称,然后将其转换为 MemoryTarget,并使用 Log 属性来获取收集的日志