Search code examples
nlogstackdrivergoogle-cloud-stackdriver

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


Solution

  • 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