Utiliser un fournisseur de journalisation Google pour le framework de journalisation standard .NET
Vous pouvez activer Cloud Logging pour les applications .NET en utilisant les bibliothèques Google.Cloud.Diagnostics pour .NET.
Pour les applications qui ciblent ASP.NET Core 3 ou une version ultérieure, utilisez la bibliothèque Google.Cloud.Diagnostics.AspNetCore3. Pour en savoir plus et obtenir des exemples, consultez la
documentation Google.Cloud.Diagnostics.AspNetCore3.
Pour les applications qui ciblent des versions antérieures d'ASP.NET Core, utilisez la bibliothèque Google.Cloud.Diagnostics.AspNetCore. Pour en savoir plus et obtenir des exemples, consultez la
documentation Google.Cloud.Diagnostics.AspNetCore.
Pour les applications qui ciblent des frameworks autres que ASP.NET Core, utilisez la bibliothèque Google.Cloud.Diagnostics.Common. Pour en savoir plus et obtenir des exemples, consultez la
documentation Google.Cloud.Diagnostics.Common.
Utiliser un fournisseur Google Log4Net
La bibliothèque Google.Cloud.Logging.Log4Net implémente un fournisseur Log4Net pour Cloud Logging. Pour obtenir des exemples montrant comment configurer et utiliser cette bibliothèque, consultez la documentation de Google.Cloud.Logging.Log4Net.
Utiliser Google.Cloud.Logging.V2 pour appeler directement l'API Logging
Vous pouvez également écrire des journaux en appelant l'API Cloud Logging à l'aide de la bibliothèque cliente Google.Cloud.Logging.V2. Vous pouvez installer cette bibliothèque à partir de NuGet.
Une fois la bibliothèque cliente Google.Cloud.Logging.V2 installée, vous pouvez commencer à envoyer les journaux de votre application à Cloud Logging. Par exemple, vous pouvez personnaliser la méthode suivante et l'ajouter au code de votre application.
Pour afficher l'exemple complet, cliquez sur more_vertPlus, puis sélectionnez Afficher sur 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}.");}
Écrivez un code de journalisation qui appelle WriteLogEntry(). L'entrée de journal créée apparaît dans l'explorateur de journaux sous la ressource Global.
Dans la console Google Cloud , accédez à la page Explorateur de journaux.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/03 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/03 (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)"]]