Usa un proveedor de registros de Google para el framework de registros estándar de .NET
Puedes habilitar Cloud Logging para aplicaciones .NET con las bibliotecas Google.Cloud.Diagnostics para .NET.
Para las aplicaciones que se orientan a ASP.NET Core 3 o versiones posteriores, usa la biblioteca Google.Cloud.Diagnostics.AspNetCore3. Consulta la
documentación de Google.Cloud.Diagnostics.AspNetCore3 para obtener más información y ejemplos.
Para las aplicaciones que se orientan a versiones anteriores de ASP.NET Core, usa la biblioteca Google.Cloud.Diagnostics.AspNetCore. Consulta la
documentación de Google.Cloud.Diagnostics.AspNetCore para obtener más información y ejemplos.
Para las aplicaciones que se orientan a plataformas que no son de ASP.NET Core, usa la biblioteca Google.Cloud.Diagnostics.Common. Consulta la
documentación de Google.Cloud.Diagnostics.Common para obtener más información y ejemplos.
Usa un proveedor de Google Log4Net
La biblioteca Google.Cloud.Logging.Log4Net implementa un proveedor de Log4Net para Cloud Logging. Para ver ejemplos que muestran cómo configurar y usar esta biblioteca, consulta la
documentación de Google.Cloud.Logging.Log4Net.
Usa Google.Cloud.Logging.V2 para llamar directamente a la API de Logging
También puedes escribir registros llamando a la API de Cloud Logging con la biblioteca cliente de Google.Cloud.Logging.V2. Puedes instalar esta biblioteca desde NuGet.
Después de instalar la biblioteca cliente de Google.Cloud.Logging.V2, puedes comenzar a enviar los registros de tu aplicación a Cloud Logging. Por ejemplo, puedes personalizar el siguiente método y agregarlo al código de tu aplicación.
Para ver la muestra completa, haz clic enmore_vertMás y, luego, selecciona Ver en 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}.");}
Escribe un código de registro que llame a WriteLogEntry(). La entrada de registro que se genere como resultado estará en el Explorador de registros en el recurso Global.
En la consola de Google Cloud , ve a la página Explorador de registros.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-09-05 (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)"]]