Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Nlog to write structured logs to Google Stackdriver

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:

enter image description here

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

result

like image 435
ManxJason Avatar asked Aug 06 '26 22:08

ManxJason


1 Answers

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

like image 119
Rolf Kristensen Avatar answered Aug 09 '26 12:08

Rolf Kristensen