serilog-sinks-googlecloudlogging
serilog-sinks-googlecloudlogging copied to clipboard
Trace is not displayed.
trafficstars
I have deployed an application developed in .Net 6 to Cloud Run. Although I have set UseLogCorrelation to True, the Trace is not being displayed in the output logs. Is there a mistake in my configuration?
Program.cs
var builder = WebApplication.CreateBuilder(args);
var config = new GoogleCloudLoggingSinkOptions { ProjectId = "MY_PROJECT_ID", UseLogCorrelation = true };
builder.Host.UseSerilog((ctx, lc) => lc
.ReadFrom.Configuration(ctx.Configuration)
.Enrich.FromLogContext()
.WriteTo.GoogleCloudLogging(config));
Controller
Log.Information("Test Log");
_logger.LogInformation("Test Log")
Log
{
"insertId": "6dzqzcg106aqd8",
"jsonPayload": {
"properties": {
"ActionId": "5dfa3659-b6c6-4a30-a1e2-6ae582176be1",
"RequestPath": "/Api",
"SourceContext": "WebAPI.Controllers.ApiController",
"RequestId": "0HN10GKCPK726:00000002",
"ActionName": "WebAPI.Controllers.ApiController.Get (WebAPI)",
"ConnectionId": "0HN10GKCPK726"
},
"message": "Test Log"
},
"resource": {
"type": "cloud_run_revision",
"labels": {
"location": "asia-east2",
"configuration_name": "logtest",
"service_name": "logtest",
"project_id": "MY_PROJECT_ID",
"revision_name": "myproject-00029-p9s"
}
},
"timestamp": "2024-01-29T04:45:22.692149600Z",
"severity": "INFO",
"logName": "projects/MY_PROJECT_ID/logs/WebAPI.Controllers.ApiController",
"receiveTimestamp": "2024-01-29T04:45:33.794937066Z"
}
I have this problem also - it's because the TraceId and SpanId fields are now top-level properties of Serilog.Events.LogEvent instead of key/value pairs inside the Properties collection, and this sink currently only looks inside the Properties collection for some hard-coded names:
foreach (var property in evnt.Properties)
{
_logFormatter.WritePropertyAsJson(propStruct, property.Key, property.Value);
HandleSpecialProperty(log, property.Key, property.Value);
}
private void HandleSpecialProperty(LogEntry log, string key, LogEventPropertyValue value)
{
if (_sinkOptions.UseLogCorrelation)
{
if (key.Equals("TraceId", StringComparison.OrdinalIgnoreCase))
log.Trace = $"projects/{_projectId}/traces/{GetString(value)}";
if (key.Equals("SpanId", StringComparison.OrdinalIgnoreCase))
log.SpanId = GetString(value);
if (key.Equals("TraceSampled", StringComparison.OrdinalIgnoreCase))
log.TraceSampled = GetBoolean(value);
}
static string GetString(LogEventPropertyValue v) => (v as ScalarValue)?.Value?.ToString() ?? "";
static bool GetBoolean(LogEventPropertyValue v) => (v as ScalarValue)?.Value is true;
}
These properties integrate with the System.Diagnostics.Activity API so this sink would need to use those for the tracing information also.