【发布时间】:2015-05-07 23:35:37
【问题描述】:
我将 NLog 用于两个目标:
<?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">
<targets async="true">
<target name="logfile" xsi:type="File" fileName="my.log"/>
<target name="console" xsi:type="Console"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile"/>
<logger name="*" minlevel="Info" writeTo="console"/>
</rules>
</nlog>
是否可以只将消息记录到“日志文件”目标,而不将消息写入“控制台”目标?
编辑
澄清一下:我想在运行时将来自同一类的消息定向到不同的记录器(无需更改 XML)。比如:
class Program
{
static Logger _logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
// Note - _logger.InfoToTarget() does not really exist
_logger.InfoToTarget("logfile", "This is my very detailed message, possibly with object dumps");
_logger.InfoToTarget("console", "Short message");
}
}
我知道这会将我的代码与 NLlog.config 文件结合在一起。
【问题讨论】: