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.
For applications that target ASP.NET Core 3 or later, use the
Google.Cloud.Diagnostics.AspNetCore3
library. See the
Google.Cloud.Diagnostics.AspNetCore3
documentation
for more information and examples.
For applications that target earlier versions of ASP.NET Core, use the
Google.Cloud.Diagnostics.AspNetCore
library. See the
Google.Cloud.Diagnostics.AspNetCore
documentation
for more information and examples.
For applications that target non ASP.NET Core platforms, use the
Google.Cloud.Diagnostics.Common
library. See the
Google.Cloud.Diagnostics.Common
documentation
for more information and examples.
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 directly 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.
After the
Google.Cloud.Logging.V2
client library is installed, you can
start sending your application's logs to Cloud Logging. For example, you
might customize the following method and add it to your application code.
To view the full sample, click more_vertMore,
and then select View on GitHub.
privatevoidWriteLogEntry(stringlogId){varclient=LoggingServiceV2Client.Create();LogNamelogName=newLogName(s_projectId,logId);varjsonPayload=newStruct(){Fields={{"name",Value.ForString("King Arthur")},{"quest",Value.ForString("Find the Holy Grail")},{"favorite_color",Value.ForString("Blue")}}};LogEntrylogEntry=newLogEntry{LogNameAsLogName=logName,Severity=LogSeverity.Info,JsonPayload=jsonPayload};MonitoredResourceresource=newMonitoredResource{Type="global"};IDictionary<string,string>entryLabels=newDictionary<string,string>
{{"size","large"},{"color","blue"}};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.
In the Google Cloud console, go to the Logs Explorer page:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[],[],null,["# Using .NET logging frameworks or calling the API\n\nThere are several ways to use Cloud Logging from your .NET application:\n\n- [Use a Google logging provider for .NET's standard logging framework](#use-standard)\n- [Use a Google Log4Net provider](#use-log4net)\n- [Use\n `Google.Cloud.Logging.V2`\n to directly call the Logging API](#use-api)\n\nUse a Google logging provider for .NET's standard logging framework\n-------------------------------------------------------------------\n\nYou can enable Cloud Logging for .NET applications by using the\n\n`Google.Cloud.Diagnostics`\nlibraries for .NET.\n\n- For applications that target ASP.NET Core 3 or later, use the `Google.Cloud.Diagnostics.AspNetCore3` library. See the [`Google.Cloud.Diagnostics.AspNetCore3`\n documentation](/dotnet/docs/reference/Google.Cloud.Diagnostics.AspNetCore3/latest#configuration-1) for more information and examples.\n- For applications that target earlier versions of ASP.NET Core, use the `Google.Cloud.Diagnostics.AspNetCore` library. See the [`Google.Cloud.Diagnostics.AspNetCore`\n documentation](/dotnet/docs/reference/Google.Cloud.Diagnostics.AspNetCore/latest#configuration-1) for more information and examples.\n- For applications that target non ASP.NET Core platforms, use the `Google.Cloud.Diagnostics.Common` library. See the [`Google.Cloud.Diagnostics.Common`\n documentation](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest#configuration-1) for more information and examples.\n\nUse a Google Log4Net provider\n-----------------------------\n\nThe\n`Google.Cloud.Logging.Log4Net`\nlibrary implements a Log4Net provider for\nCloud Logging. For examples that show how to configure and use\nthis library, see the\n[`Google.Cloud.Logging.Log4Net`\ndocumentation](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/latest).\n\nUse\n`Google.Cloud.Logging.V2`\nto directly call the Logging API\n--------------------------------------------------------------\n\nYou can also write logs by calling the Cloud Logging API using\nthe\n`Google.Cloud.Logging.V2`\nclient library. You can install this library from\n[NuGet](https://www.nuget.org/packages/Google.Cloud.Logging.V2/).\n\nAfter the\n`Google.Cloud.Logging.V2`\nclient library is installed, you can\nstart sending your application's logs to Cloud Logging. For example, you\nmight customize the following method and add it to your application code.\nTo view the full sample, click *more_vert* **More** ,\nand then select **View on GitHub**. \n\n private void WriteLogEntry(string logId)\n {\n var client = LoggingServiceV2Client.Create();\n LogName logName = new LogName(s_projectId, logId);\n var jsonPayload = new Struct()\n {\n Fields =\n {\n { \"name\", Value.ForString(\"King Arthur\") },\n { \"quest\", Value.ForString(\"Find the Holy Grail\") },\n { \"favorite_color\", Value.ForString(\"Blue\") }\n }\n };\n LogEntry logEntry = new LogEntry\n {\n LogNameAsLogName = logName,\n Severity = LogSeverity.Info,\n JsonPayload = jsonPayload\n };\n MonitoredResource resource = new MonitoredResource { Type = \"global\" };\n IDictionary\u003cstring, string\u003e entryLabels = new Dictionary\u003cstring, string\u003e\n {\n { \"size\", \"large\" },\n { \"color\", \"blue\" }\n };\n client.WriteLogEntries(logName, resource, entryLabels,\n new[] { logEntry }, _retryAWhile);\n Console.WriteLine($\"Created log entry in log-id: {logId}.\");\n }\n\nWrite some logging code that calls `WriteLogEntry()`. The resulting log entry\nwill be in the Logs Explorer under the **Global** resource.\n\nIn the Google Cloud console, go to the **Logs Explorer** page:\n\n[Go to **Logs Explorer**](https://console.cloud.google.com/logs/query)\n\n\u003cbr /\u003e\n\nIf you use the search bar to find this page, then select the result whose subheading is\n**Logging**.\n\nResources\n---------\n\n- For details on the Logs Explorer, see \\[Using the Logs Explorer\\]\\[view-ui\\].\n- For ASP.NET Core 3+ applications\n - [Documentation: `Google.Cloud.Diagnostics.AspNetCore3`](/dotnet/docs/reference/Google.Cloud.Diagnostics.AspNetCore3/latest#configuration-1)\n - [NuGet: `Google.Cloud.Diagnostics.AspNetCore3` package](https://www.nuget.org/packages/Google.Cloud.Diagnostics.AspNetCore3/)\n- For earlier ASP.NET Core applications\n - [Documentation: `Google.Cloud.Diagnostics.AspNetCore`](/dotnet/docs/reference/Google.Cloud.Diagnostics.AspNetCore/latest#configuration-1)\n - [NuGet: `Google.Cloud.Diagnostics.AspNetCore` package](https://www.nuget.org/packages/Google.Cloud.Diagnostics.AspNetCore/)\n- For non ASP.NET Core applications\n - [Documentation: `Google.Cloud.Diagnostics.Common`](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest#configuration-1)\n - [NuGet: `Google.Cloud.Diagnostics.Common` package](https://www.nuget.org/packages/Google.Cloud.Diagnostics.Common/)\n- Log4Net Provider\n - [Documentation: `Google.Cloud.Logging.Log4Net`](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/latest)\n - [NuGet: `Google.Cloud.Logging.Log4Net` package](https://www.nuget.org/packages/Google.Cloud.Logging.Log4Net/)\n- [GitHub: googleapis/google-cloud-dotnet](https://github.com/googleapis/google-cloud-dotnet)\n- [GitHub issue tracker](https://github.com/googleapis/google-cloud-dotnet/issues)"]]