Google Logging-Anbieter für das Standard-Logging-Framework von .NET verwenden
Sie können Cloud Logging für .NET-Anwendungen mithilfe der Google.Cloud.Diagnostics-Bibliotheken für .NET aktivieren.
Verwenden Sie für Anwendungen, die auf ASP.NET Core 3 oder höher ausgerichtet sind, die Bibliothek Google.Cloud.Diagnostics.AspNetCore3. Weitere Informationen und Beispiele finden Sie in der
Dokumentation zu Google.Cloud.Diagnostics.AspNetCore3.
Verwenden Sie für Anwendungen, die auf ältere Versionen von ASP.NET Core abzielen, die Google.Cloud.Diagnostics.AspNetCore-Bibliothek. Weitere Informationen und Beispiele finden Sie in der
Dokumentation zu Google.Cloud.Diagnostics.AspNetCore.
Verwenden Sie für Anwendungen, die nicht auf ASP.NET Core-Plattformen abzielen, die Google.Cloud.Diagnostics.Common-Bibliothek. Weitere Informationen und Beispiele finden Sie in der
Dokumentation zu Google.Cloud.Diagnostics.Common.
Google Log4Net-Anbieter verwenden
Die Bibliothek Google.Cloud.Logging.Log4Net implementiert einen Log4Net-Anbieter für Cloud Logging. Beispiele zum Konfigurieren und Verwenden dieser Bibliothek finden Sie in der
Dokumentation zu Google.Cloud.Logging.Log4Net.
Verwenden Sie
Google.Cloud.Logging.V2
, um die Logging API direkt aufzurufen.
Sie können Logs auch schreiben, wenn Sie die Cloud Logging API mithilfe der Google.Cloud.Logging.V2-Clientbibliothek aufrufen. Sie können diese Bibliothek über NuGet installieren.
Nachdem die Clientbibliothek Google.Cloud.Logging.V2 installiert wurde, können Sie die Anwendungslogs an Cloud Logging senden. Sie können beispielsweise die folgende Methode anpassen und Ihrem Anwendungscode hinzufügen.
Wenn Sie das vollständige Beispiel sehen möchten, klicken Sie auf more_vertMehr und wählen Sie dann Auf GitHub ansehen aus.
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}.");}
Schreiben Sie Logging-Code, der WriteLogEntry() aufruft. Der resultierende Logeintrag ist im Log-Explorer unter der Ressource Global zu sehen.
Rufen Sie in der Google Cloud Console die Seite Log-Explorer auf:
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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)"]]