如何在 Stackdriver 中以编程方式将 NLog 与 JsonPayload 结合使用

问题描述 投票:0回答:2

我正在尝试使用带有 JsonLayout 和 JsonPayload 的 NLog 将日志发送到 Stackdriver。但是,我的 JsonAttributes 没有在 JsonPayload 中显示为单独的字段/标签。当前有效负载中显示的唯一属性是“消息”。

这是我的目标代码:

            var stackdriverTarget = new GoogleStackdriverTarget
            {
                ProjectId = "development-212820",
                Name = "stackdriver",
                LogId = "science",
                IncludeEventProperties = true, 
                Layout = new JsonLayout
                {
                    Attributes =
                    {
                        new JsonAttribute("message", "${message}",false),
                        new JsonAttribute("exception", "${exception:format=toString}",false)
                    }
                },
                SendJsonPayload = true
            };

最终在 GCP 中显示为 this

我假设我在这里做错了什么,但我一生都无法弄清楚那是什么。我之前使用过 GCP 日志客户端库,这些类型的东西通常只是标签,但我想利用 NLog 和 JsonPayload。 我看过堆栈文章使用Nlog将结构化日志写入Google Stackdriver,但它没有回答我的问题。

c# json nlog stackdriver
2个回答
3
投票

尝试像这样使用

ContextProperties

        var stackdriverTarget = new GoogleStackdriverTarget
        {
            ProjectId = "development-212820",
            Name = "stackdriver",
            LogId = "science",
            Layout = "${message}",
            SendJsonPayload = true,
            IncludeEventProperties = true, 
            ContextProperties = { new TargetPropertyWithContext("exception", "${exception:format=toString}") },
        };

如果您想完全覆盖 JsonPayload(对性能有影响),那么您可以升级到 Google.Cloud.Logging.NLog 版本。 3.2.0并激活EnableJsonLayout


0
投票

我知道这是一篇旧帖子,但请允许我做出贡献。

[TL博士] 无需使用 JsonLayout 或任何其他,只需设置 contextProperties 即可。 [/TL 博士]

如果您想在 Google Logs(或旧名称 Stackdriver)中生成 JSON 对象结构化日志而不是 JSON 字符串日志,则必须阅读此内容 -> https://github.com/googleapis/google-云-dotnet/问题/3664

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.