Menggunakan penyedia logging Google untuk framework logging standar .NET
Anda dapat mengaktifkan Cloud Logging untuk aplikasi .NET menggunakan library
Google.Cloud.Diagnostics
untuk .NET.
Untuk aplikasi yang menargetkan ASP.NET Core 3 atau yang lebih baru, gunakan library
Google.Cloud.Diagnostics.AspNetCore3. Lihat
Google.Cloud.Diagnostics.AspNetCore3
dokumentasi
untuk mengetahui informasi dan contoh selengkapnya.
Untuk aplikasi yang menargetkan ASP.NET Core versi yang lebih lama, gunakan
Google.Cloud.Diagnostics.AspNetCore
library. Lihat
Google.Cloud.Diagnostics.AspNetCore
dokumentasi
untuk mengetahui informasi dan contoh selengkapnya.
Untuk aplikasi yang menargetkan platform non-ASP.NET Core, gunakan library
Google.Cloud.Diagnostics.Common. Lihat
Google.Cloud.Diagnostics.Common
dokumentasi
untuk mengetahui informasi dan contoh selengkapnya.
Menggunakan penyedia Google Log4Net
Library
Google.Cloud.Logging.Log4Net
menerapkan penyedia Log4Net untuk
Cloud Logging. Untuk contoh yang menunjukkan cara mengonfigurasi dan menggunakan library ini, lihat dokumentasi Google.Cloud.Logging.Log4Net.
Gunakan
Google.Cloud.Logging.V2
untuk memanggil Logging API secara langsung
Anda juga dapat menulis log dengan memanggil Cloud Logging API menggunakan
library klien Google.Cloud.Logging.V2. Anda dapat menginstal library ini dari
NuGet.
Setelah library klien
Google.Cloud.Logging.V2
diinstal, Anda dapat
mulai mengirim log aplikasi ke Cloud Logging. Misalnya, Anda
dapat menyesuaikan metode berikut dan menambahkannya ke kode aplikasi Anda.
Untuk melihat contoh lengkap, klik more_vertLainnya,
lalu pilih Lihat di 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}.");}
Tulis beberapa kode logging yang memanggil WriteLogEntry(). Entri log yang dihasilkan
akan berada di Logs Explorer di bagian resource Global.
Di konsol Google Cloud , buka halaman Logs Explorer:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)"]]