You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NLog.Targets.Seq无法解析第三方库Composite Formatting问题求助

解决NLog.SeqTarget无法解析Composite Formatting占位符的问题

方案1:配置SeqTarget使用${message}布局

SeqTarget默认会保留原始消息模板和参数,但对于{0}、{1}这类Composite Formatting风格的占位符,需要让NLog先完成消息格式化再发送到Seq。你可以通过修改NLog配置,为SeqTarget指定${message}布局来实现:

XML配置示例

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target xsi:type="Seq" 
            name="seqTarget" 
            serverUrl="http://your-seq-server:5341"
            apiKey="your-api-key">
      <!-- 用${message}触发NLog解析占位符 -->
      <layout>${message}</layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="seqTarget" />
  </rules>
</nlog>

C#代码配置示例

var config = new LoggingConfiguration();
var seqTarget = new SeqTarget
{
    Name = "seqTarget",
    ServerUrl = "http://your-seq-server:5341",
    ApiKey = "your-api-key",
    // 直接指定布局为${message}
    Layout = new SimpleLayout("${message}")
};
config.AddTarget(seqTarget);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, seqTarget));
LogManager.Configuration = config;

${message}布局会自动将日志调用传入的参数替换到占位符中,生成完整格式化消息后再发送到Seq。

方案2:同时保留格式化消息与结构化参数

如果需要在Seq中既查看完整消息,又能基于原始参数检索,可以自定义布局同时输出两者:

<layout>
  ${message}${newline}Params: ${all-event-properties}
</layout>

也可以开启NLog的结构化日志支持,自动将Composite Formatting的参数转为Seq可检索的属性:

<nlog>
  <variable name="structuredLogging" value="true" />
  <!-- 其他配置内容 -->
</nlog>

关键说明

SeqTarget本身支持自定义布局,你觉得“无法重写布局”大概率是配置方式有误。只要在XML配置中明确指定<layout>节点,或在代码里直接赋值Layout属性,就能覆盖默认行为,触发NLog对Composite Formatting占位符的解析。

内容的提问来源于stack exchange,提问作者David Montgomery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 08:37:38