There are three ways to use the Cloud Logging client library:
- Option 1: Using an ASP.NET core integration
- Option 2: Using a Log4Net integration with ASP.NET Framework
- Option 3: Writing custom logs using the Logging API
Option 1: Using an ASP.NET Core integration
Google.Cloud.Diagnostics.AspNetCore is a library that sends your standard ASP.NET logs to Cloud Logging.
To start logging using Google.Cloud.Diagnostics.AspNetCore, install the library with one of these commands:
Package Manager
PM> Install-Package Google.Cloud.Diagnostics.AspNetCore
.NET CLI
> dotnet add package Google.Cloud.Diagnostics.AspNetCore
Then, edit your Startup.cs
file with the following changes:
Add the following using statements to the top of
Startup.cs
:Modify the
Configure
function. Add aILoggerFactory loggerFactory
and add a call tologgerFactory.AddGoogle("YOUR-PROJECT-ID")
:Compile and run your code in Visual Studio. See the log entries in the Logs Explorer under the Global resource.
Option 2: 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 library with this command:
PM> Install-Package Google.Cloud.Logging.Log4Net
Then do the following:
Add a Log4Net XML configuration section to your web application's
Web.config
file containing the following code:Add the following line of code to your application's Global.asax.cs file to configure Log4net to use Logging:
log4net.Config.XmlConfigurator.Configure();
Once you've added the previous line to your code, change the
Application_Start()
method inGlobal.asax.cs
to the following:To use log4net in your application code, add the following statement to include the client library:
using log4net;
Add the following code to your application to write logs that will appear in the Logs Explorer under the Global resource. The log name is specified in the
Web.config
file:Compile and run your code in Visual Studio. See the log entries in the Logs Viewer of Logging under the Global resource.
Option 3: Writing custom logs using the Logging API
You can alternatively write custom logs by directly calling the Cloud Logging API.
To start logging using Google.Cloud.Logging.V2, install the library with one of these commands:
Package Manager
PM> Install-Package Google.Cloud.Logging.V2
.NET CLI
> dotnet add package Google.Cloud.Logging.V2
Once the Logging client is installed, you can start sending your application's logs to 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:
Write some logging code that calls WriteLogEntry()
. The resulting log entry
will be in the Logs Explorer under the Global resource:
For details on the Logs Explorer, see Using the Logs Explorer.