Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace is not displayed. #67

Open
nana7038 opened this issue Jan 29, 2024 · 2 comments
Open

Trace is not displayed. #67

nana7038 opened this issue Jan 29, 2024 · 2 comments

Comments

@nana7038
Copy link

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"
}
@adamrodger
Copy link

adamrodger commented Mar 7, 2024

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.

StasPerekrestov pushed a commit to StasPerekrestov/serilog-sinks-googlecloudlogging that referenced this issue Mar 14, 2024
Serilog starting from v3.1.0 adds two new first-class properties to LogEvent: TraceId and SpanId. These are set automatically in Logger.Write() to the corresponding property values from System.Diagnostics.Activity.Current.
StasPerekrestov pushed a commit to StasPerekrestov/serilog-sinks-googlecloudlogging that referenced this issue Jun 3, 2024
Serilog starting from v3.1.0 adds two new first-class properties to LogEvent: TraceId and SpanId. These are set automatically in Logger.Write() to the corresponding property values from System.Diagnostics.Activity.Current.
@StasPerekrestov
Copy link
Contributor

@vadimt2 as we don't have a maintainer at the moment, you can try building your version of the sink from the PR's version #72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants