Provide 'structure' log ability in Google.Cloud.Diagnostics.AspNetCore
See original GitHub issueMicrosoft.Extensions.Logging provide structure logging out of box MS docs But this feature should be supported in ILogger implementation.
I see that google logs support several payloads for log entry, but for logging used simple textPayload which hold bare text. I found what GoogleExceptionLogger used jsonPayload to store information in more useful way.
So, I make little experiment: rewrite GoogleLogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) method:
...
var jsonPayload = new Struct
{
Fields = { { "Message", Value.ForString(message) } }
};
if (GoogleLoggerScope.Current != null)
{
jsonPayload.Fields.Add("Scope", Value.ForString(GoogleLoggerScope.Current.ToString()));
}
if (state is IEnumerable<KeyValuePair<string, object>> structure)
{
var properties = new Struct();
foreach (var pair in structure)
{
if (pair.Key.Equals(OriginalFormatKeyValue))
{
continue;
}
properties.Fields.Add(pair.Key, Value.ForString(pair.Value.ToString()));
}
if (properties.Fields.Count > 0)
{
jsonPayload.Fields.Add("Properties", Value.ForStruct(properties));
}
}
if (eventId.Id != 0 || eventId.Name != null)
{
var eventStruct = new Struct();
if (eventId.Id != 0)
{
eventStruct.Fields.Add("Id", Value.ForNumber(eventId.Id));
}
if (!string.IsNullOrEmpty(eventId.Name))
{
eventStruct.Fields.Add("Name", Value.ForString(eventId.Name));
}
jsonPayload.Fields.Add("EventId", Value.ForStruct(eventStruct));
}
LogEntry entry = new LogEntry
{
Resource = _loggerOptions.MonitoredResource,
LogName = _logName,
Severity = logLevel.ToLogSeverity(),
Timestamp = Timestamp.FromDateTime(_clock.GetCurrentDateTimeUtc()),
JsonPayload = jsonPayload,
Labels = { _loggerOptions.Labels },
Trace = GetTraceName() ?? "",
};
...
and that send log w/o any error to GCP (also put eventId information).
Also I have configured out sink to BigQuery, and jsonPayload fit to table perfectly as record.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Google.Cloud.Diagnostics.AspNetCore
This configures the Logging, Tracing and Error Reporting components, including neccesarry middlewares. If your application is running on Google ...
Read more >Google.Cloud.Diagnostics.Common
NET Core instrumentation library for Google Logging, Error Reporting and Tracing. It allows for simple integration of Google observability ...
Read more >New to GCP and Cloud Run and pretty new to .Net. Can't ...
I've decided to use Google.Cloud.Logging.Console and it seems to get the job done. The docs show the couple of lines that you have...
Read more >Google Cloud Platform Blog - googblogs.com
Logging to Stackdriver is simple with Google.Cloud.Diagnostics.AspNetCore. The package uses ASP.NET Core's built in logging API; ...
Read more >Google.Cloud.Diagnostics.AspNet
Google.Cloud.Diagnostics.AspNet is an ASP.NET instrumentation library for Google Stackdriver. It allows for simple integration of Stackdriver into ASP.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Code complete and will be in our next major release w/in the next month or so.
leave for future … this is atom feed for subscribe https://github.com/GoogleCloudPlatform/google-cloud-dotnet/releases.atom