Using Nlog to write structured logs to Google Stackdriver

有些话、适合烂在心里 提交于 2021-01-27 15:04:26

问题


I'm using the Google.Cloud.Logging.NLog target to write logs to Stackdriver. I'd like to use Nlog & Stackdriver structured logging capabilities by sending a JSON payload as per the Stackdriver documentation:

Logs are coming through but I don't seem to be populating the jsonPayload property in the logs, which will mean that I'm not sending a JSON payload. Do I need to configure a JsonLayout for every type of log message stated below?

Target code

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()
};

Logs

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");

Result in Stackdriver


回答1:


I have created the following PR, that adds support for sending Google JsonPayload:

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

Update That now have evolved into an official nuget-package:

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



来源:https://stackoverflow.com/questions/50985439/using-nlog-to-write-structured-logs-to-google-stackdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!