dotnet-docs-samples icon indicating copy to clipboard operation
dotnet-docs-samples copied to clipboard

Missing example of writing a structured log entry

Open hultqvist opened this issue 3 years ago • 2 comments

The C# example of writing structured logs doesn't look like it's working with JSON payload.

https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/ed6f1b739c8cf1589f2f11e3eb0c6bcd4a97a48b/logging/api/LoggingSample/Program.cs#L79

I'm looking for an example to set the LogEntry.JsonPayload but it's not clear how to generate the Google.Protobuf.WellKnownTypes.Struct.

hultqvist avatar Jun 11 '22 14:06 hultqvist

I've marked this as a bug because that sample needs to be updated to be really stuctured or else removed from the structured logging documentation.

I don't have a sample to share with you but I can give you a few pointers:

  • We recently released Google.Cloud.Logging.Console (still a beta) that will integrate with Microsoft.Extensions.Logging and performs structured logging. See the reference docs.
  • A less lightweight option, as it also performs tracing and error reporting would be to use Google.Cloud.Diagnostics.Commom for non ASP.NET Core applications and Google.Cloud.Diagnostics.AspNetCore3 for ASP.NET Core 3 or latest apps. The logging component will also perform structured logging.
  • If you still want to use the API directly through the client library, I have no example for you, but here's some of the Diagnostics library code were we build the JSON Payload

amanda-tarafa avatar Jun 13 '22 08:06 amanda-tarafa

Since I was looking for logging custom data I created the custom Struct.

This example is using the same data as in the example in Java:

var json = new Struct();
json.Fields.Add("name", Value.ForString("King Arthur"));
json.Fields.Add("quest", Value.ForString("Find the Holy Grail"));
json.Fields.Add("favorite_color", Value.ForString("Blue"));

logEntry.JsonPayload = new Struct();

hultqvist avatar Jun 13 '22 11:06 hultqvist