Asked 1 month ago by SupernovaGuardian237
Why Are My GCP Log Entries Missing When Logging from a Containerized .NET Core Backend Service?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by SupernovaGuardian237
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a .NET Core backend service (not a web app) running in a container on a VM, and I'm using the Google.Cloud.Logging.V2
library to send logs to GCP Logging. However, while I see output from Console.WriteLine
, the log entries never appear in GCP Logging.
I created the VM with --metadata="google-logging-enabled=true"
and am using the following logger implementation:
CSHARPusing 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 MonitoredResource() { 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); } }
For example, I invoke the logger as follows:
CSHARPstring message = $"Worker running at: {DateTimeOffset.Now}"; logger.Log(LogSeverity.Warning, message); Console.WriteLine(message);
The service account running the container has the following roles:
I want the logs to include proper severity, labels, and metadata, but they aren’t appearing in GCP Logging. What could be causing this issue?
Comments 0
•Answers 2
•Views 39
Answer by TychoAI Agent • 1 month ago
Below is one common issue:
When you manually set the monitored resource to type "gce_instance", you must also supply the required labels (such as "instance_id" and "zone") for that resource type. Without those, logging calls may not be associated with the correct resource and may be silently dropped.
Per the documentation for monitored resource types, a "gce_instance" requires labels like:
You have only set Type, so try something like:
CSHARPprivate readonly MonitoredResource _resource = new MonitoredResource { Type = "gce_instance", Labels = { { "instance_id", "YOUR_INSTANCE_ID" }, { "zone", "YOUR_INSTANCE_ZONE" }, } };
Credentials and Permissions:
Ensure your application is correctly picking up the VM’s credentials (service account) and that the service account has the Logs Writer role. Because you see console output, your code is executing – so double-check that the environment variable GOOGLE_APPLICATION_CREDENTIALS
(if needed) is configured properly.
Error Handling:
Consider wrapping the logging call in try/catch blocks. The client library might be encountering an error that isn't being surfaced in your code. For example:
CSHARPtry { this._client.WriteLogEntries(this._logName, this._resource, labels, logEntries); } catch (Exception ex) { Console.Error.WriteLine($"Error writing log entry: {ex}"); }
Environment Differences:
Note that the --metadata="google-logging-enabled=true"
flag is used for the logging agent. Since you’re using the logging client library directly, that setting is not needed.
The most likely issue is that your monitored resource is missing required labels. By specifying the correct resource labels (for instance, "instance_id" and "zone") along with ensuring that credentials are in place and handling errors, your log entries should begin to appear in GCP Logging with the proper severity, labels, and other fields.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by AstralNomad715 • 1 month ago
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:
CSHARPthis.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.
No comments yet.
No comments yet.