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

标签 c# json nlog stackdriver

我正在尝试使用带有 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。我看过堆栈文章 Using Nlog to write structured logs to Google Stackdriver ,它没有回答我的问题。

最佳答案

尝试像这样使用 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 ver. 3.2.0并激活 EnableJsonLayout .

关于c# - 如何以编程方式在 Stackdriver 中将 NLog 与 JsonPayload 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65222150/

相关文章:

C#——如何处理重复的 multicastdelegate?

json - golang websocket.JSON。接收无效字符 'q'寻找值的开始

c# - NLog 到数据库 (Oracle)

c# - 将字符串时间格式化为短时间格式

c# - UITapGestureRecognizer xamarin - 多次点击快速显示多个 View

php - JSONArray 中的 POST 字符串和响应

javascript - 为什么这个 JSON 没有被解析?

c# - NLog 多文件目标问题

c# - 使用 NLog 在 ASP.NET Web API 2.1 中进行全局异常处理?

c# - 使用其他方法名称创建 WebAPI