使用Nlog将结构化日志写入Google Stackdriver

时间:2018-06-22 09:57:39

标签: nlog stackdriver google-cloud-stackdriver

我正在使用Google.Cloud.Logging.NLog目标将日志写入Stackdriver。 我想通过按照Stackdriver documentation发送JSON有效负载来使用Nlog和Stackdriver结构化的日志记录功能:

enter image description here

日志通过,但我似乎没有在日志中填充 jsonPayload 属性,这意味着我没有发送JSON有效负载。我需要configure a JsonLayout来处理以下每种类型的日志消息吗?

目标代码

GoogleStackdriverTarget googleTarget = new GoogleStackdriverTarget
{
    ProjectId = "123456",
    Name = "desktop app",
    CredentialFile = Path.Combine(@"my path to the json file"),
    ContextProperties = { new TargetPropertyWithContext{Name = "MyCustomContextProperty", 
    Organization.Id}},
    IncludeEventProperties = true,
    Layout = new JsonLayout()
};

日志

LogManager.GetCurrentClassLogger().Info("Logon by {user} from {ip_address}", "Kenny", "127.0.0.1");

LogManager.GetCurrentClassLogger().Info("{shopitem} added to basket by {user}", new { Id = 6, Name = "Jacket", Color = "Orange" }, "Kenny");

Stackdriver中的结果

result

1 个答案:

答案 0 :(得分:2)

我创建了以下PR,添加了对发送Google JsonPayload的支持:

https://github.com/GoogleCloudPlatform/google-cloud-dotnet/pull/2256

更新,现在已经演变为官方的nuget软件包:

https://www.nuget.org/packages/Google.Cloud.Logging.NLog