【问题标题】:NLog: is it possible to change source name from code?NLog:是否可以从代码更改源名称?
【发布时间】:2012-05-26 01:04:36
【问题描述】:

是否可以通过代码更改source 名称?我的解决方案有几个模块,我想在查看日志时将它们分开。

<?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>
    <target xsi:type="EventLog" name="eventlog" layout="${message}" log="Application" source="MyAwesomeSolution"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="eventLog" />
  </rules>
</nlog>

另外,如何将对象记录为 xml?例如如果我这样做 - logger.Info("Results - {0}", person); 我想在我的事件日志中以 XML 形式查看 person 对象。有可能吗?

【问题讨论】:

    标签: c# .net nlog


    【解决方案1】:

    用

    初始化你的 Logger 对象
    private static Logger logger = LogManager.GetCurrentClassLogger();
    

    然后你可以添加

    ${callsite} 配置文件中的布局

    例如

    <targets>
        <target xsi:type="EventLog" name="eventlog" layout="${callsite}:${message}" log="Application" source="MyAwesomeSolution"/>
    </targets>
    

    更多信息@${callsite} 是here。

    【讨论】:

    • 谢谢。我试试看。
    猜你喜欢
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2018-10-12
    • 2016-08-28
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多