Using .NET logging frameworks or calling the API

Stay organized with collections Save and categorize content based on your preferences.

There are several ways to use Cloud Logging from your .NET application:

Use a Google logging provider for .NET's standard logging framework

You can enable Cloud Logging for .NET applications by using the Google.Cloud.Diagnostics libraries for .NET.

Use a Google Log4Net provider

The Google.Cloud.Logging.Log4Net library implements a Log4Net provider for Cloud Logging. For examples that show how to configure and use this library, see the Google.Cloud.Logging.Log4Net documentation.

Use Google.Cloud.Logging.V2 to directlly call the Logging API

You can also write logs by calling the Cloud Logging API using the Google.Cloud.Logging.V2 client library. You can install this library from [NuGet][lib-nuget].

After the Google.Cloud.Logging.V2 is installed, you can start sending your application's logs to Cloud Logging by adding the following statements to your application code:

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

Customize the following method and add it to your application code:

private void WriteLogEntry(string logId, string message)
{
    var client = LoggingServiceV2Client.Create();
    LogName logName = new LogName(s_projectId, logId);
    LogEntry logEntry = new LogEntry
    {
        LogNameAsLogName = logName,
        Severity = LogSeverity.Info,
        TextPayload = $"{typeof(LoggingSample).FullName} - {message}"
    };
    MonitoredResource resource = new MonitoredResource { Type = "global" };
    IDictionary<string, string> entryLabels = new Dictionary<string, string>
    {
        { "size", "large" },
        { "color", "red" }
    };
    client.WriteLogEntries(logName, resource, entryLabels,
        new[] { logEntry }, _retryAWhile);
    Console.WriteLine($"Created log entry in log-id: {logId}.");
}

Write some logging code that calls WriteLogEntry(). The resulting log entry will be in the Logs Explorer under the Global resource:

Go to the Logs Explorer

Resources