APM NLog 布局编辑

允许您在 NLog 模板中添加以下占位符

  • ElasticApmTraceId
  • ElasticApmTransactionId
  • ElasticApmSpanId
  • ElasticApmServiceName
  • ElasticApmServiceNodeName
  • ElasticApmServiceVersion

如果可用,这些占位符将被相应的 Elastic APM 变量替换

安装编辑

添加对 Elastic.Apm.NLog 包的引用

<PackageReference Include="Elastic.Apm.NLog" Version="8.6.0" />

用法编辑

如何从 API 使用编辑

// Logged message will be in format of `trace-id|transation-id|span-id|InTransaction`
// or `|||InTransaction` if the place holders are not available
var consoleTarget = new ConsoleTarget("console");
consoleTarget.Layout =
    "${ElasticApmServiceName}|${ElasticApmTraceId}|${ElasticApmTransactionId}|${ElasticApmSpanId}|${message}";
config.AddRule(LogLevel.Debug, LogLevel.Fatal, consoleTarget);
LogManager.Configuration = config;
var logger = LogManager.GetCurrentClassLogger();

如何从 NLog.config 使用编辑

<nlog>
  <extensions>
    <add assembly="Elastic.Apm.NLog"/>
  </extensions>
  <targets>
    <target name="console"
        type="console"
        layout="${ElasticApmTraceId}|${ElasticApmTransactionId}|${ElasticApmSpanId}|${message}" />
  </targets>
  <rules>
    <logger name="*" minLevel="Debug" writeTo="Console" />
  </rules>
</nlog>

先决条件编辑

为了使此功能正常工作,需要配置一个 Elastic APM 代理。如果代理未配置,APM 占位符将为空。