Using Log4Net or calling the API

Google.Cloud.Logging.V2 is a .NET client library that allows you to write custom logs using the Stackdriver Logging API v2.

Installing the Stackdriver Logging client library

To use the Google.Cloud.Logging.V2 client library:

  1. Right-click your solution in Visual Studio and select Manage Nuget packages for solution.
  2. Select the Include prerelease checkbox.
  3. Search for and install the package named Google.Cloud.Logging.V2.

There are two ways to use the Logging client library:

Option 1: Using a Log4Net integration

Log4Net is an Apache library that lets you output log statements to a variety of output targets.

To start logging using the Log4Net integration, install the Google.Cloud.Logging.Log4Net package in the same way as you installed the Google.Cloud.Logging.V2 client library NuGet package above. Then do the following:

  1. Add a Log4Net XML configuration section to your web application's Web.config file containing the following code:

        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
        <appender name="CloudLogger" type="Google.Cloud.Logging.Log4Net.GoogleStackdriverAppender,Google.Cloud.Logging.Log4Net">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message" />
          <projectId value="YOUR-PROJECT-ID" />
          <logId value="mySampleLog" />
          <level value="ALL" />
          <appender-ref ref="CloudLogger" />

  2. Add the following line of code to your application's Global.asax.cs file to configure Log4net to use Stackdriver Logging:


    Once you've added the previous line to your code, change the Application_Start() method in Global.asax.cs to the following:

            protected void Application_Start()
                // Configure log4net to use Stackdriver logging from the XML configuration file.

  3. To use log4net in your application code, add the following statement to include the client library:

    using log4net;
  4. Add the following code to your application to write logs that will appear in the Stackdriver Logs Viewer under the Global resource. The log name is specified in the Web.config file:

                        // Retrieve a logger for this context.
                        ILog log = LogManager.GetLogger(typeof(WebApiConfig));
                        // Log some information to Google Stackdriver Logging.
                        log.Info("Hello World.");

  5. Compile and run your code in Visual Studio. See the log entries in the Logs Viewer of Stackdriver Logging under the Global resource.

Go to the Logs Viewer

Option 2: Writing custom logs using the Stackdriver Logging API client

You can alternatively write custom logs directly using the Stackdriver Logging API client without using Log4Net. Once the Stackdriver Logging client is installed, you can start sending your application's logs to Stackdriver 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
        LogName = logName.ToString(),
        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(LogNameOneof.From(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 Viewer under the Global resource:

Go to the Logs Viewer

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Logging