google-cloud-platform.net-corelogginggoogle-compute-engine

Logging from a .NET Core app to GCP is not working


I have a .NET Core application (not web app but just a backend service), using Google.Cloud.Logging.V2 to log, but I get no log entries in GCP logging.

The app is containerized and runs on a VM as a container. When I created the VM, I specified --metadata="google-logging-enabled=true".

This is the logger code I use:

using Google.Api;
using Google.Cloud.Logging.Type;
using Google.Cloud.Logging.V2;

public interface ILogger<TType>
{
    void Log(LogSeverity severity, string message, IDictionary<string, string>? labels = null);
}

public class Logger<TType> : ILogger<TType>
{
    private const string ProjectId = "myproject";

    private readonly LoggingServiceV2Client _client = LoggingServiceV2Client.Create();
    private readonly LogName _logName = new(ProjectId, typeof(TType).FullName);

    private readonly MonitoredResource _resource = new()
    {
        Type = "gce_instance"
    };

    public void Log(LogSeverity severity, string message, 
        IDictionary<string, string>? labels = null)
    {
        string messageId = DateTime.Now.Millisecond.ToString();
        string entrySeverity = severity.ToString().ToUpper();

        var logEntry = new LogEntry
        {
            LogNameAsLogName = this._logName,
            Severity = severity,
            TextPayload = $"{messageId} {entrySeverity} {typeof(TType).Namespace} - {message}"
        };

        // Add log entry to collection for writing. Multiple log entries can be added.
        IEnumerable<LogEntry> logEntries = [logEntry];
        this._client.WriteLogEntries(this._logName, this._resource, labels, logEntries);
    }
}

But if I do this in my working code:

string message = $"Worker running at: {DateTimeOffset.Now}";
logger.Log(LogSeverity.Warning, message);
Console.WriteLine(message);

I can see the Console.WriteLine entry, but not the logger entry. I want the proper logging so I can have proper severity, labels and other fields.

The service account running the VM container has the following roles:

Any idea what could go wrong here?


Solution

  • I've figured it out and I'm updating here in case anyone has the same issue.

    The logs were being sent to Cloud Logging, but they were filtered out because they didn't have enough information. When you want to see logs for a VM, the automatic view filters by project ID, region and VM ID. I wrongly assumed those labels were automatically added to the resource data, but they were not.

    To have proper information, I changed the logger to do this:

            this.Resource = new MonitoredResource
            {
                Type = this.Type,
                Labels =
                {
                    { "project_id", this.ProjectId },
                    { this.NameLabel, this.Name },
                    { "location", this.Location }
                }
            };
    

    And then the entries were labelled and filtered correctly.