最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google cloud platform - Logging from a .NET Core app to GCP is not working - Stack Overflow

programmeradmin1浏览0评论

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:

  • Artifact Registry Reader
  • Logs Writer
  • Monitoring Metric Writer
  • Storage Object Viewer

Any idea what could go wrong here?

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:

  • Artifact Registry Reader
  • Logs Writer
  • Monitoring Metric Writer
  • Storage Object Viewer

Any idea what could go wrong here?

Share Improve this question edited Feb 2 at 15:46 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 2 at 12:03 Alex PopescuAlex Popescu 711 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论