I have a dotnet 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 worker 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?