如何在 C# 中使用自訂記錄

如何在 C# 中使用自訂日誌記錄

This article was translated from English: Does it need improvement?
Translated
View the article in English

自訂日誌記錄是指實施針對應用程式或系統的特定需求和要求量身定制的日誌記錄系統的做法。 它涉及創建和使用日誌檔案來記錄軟體在運行過程中產生的資訊、事件和訊息。

快速入門:使用 IronPDF 實現自訂日誌記錄

使用 IronPDF,快速上手 C# 自訂日誌記錄。 本指南將向您展示如何將LoggingMode設定為Custom並將其與您自訂的日誌記錄器類別關聯起來。 透過將 IronPDF 訊息定向到自訂日誌記錄器,有效地監控和管理應用程式日誌。 它設定簡單,可讓您根據自身獨特需求增強應用程式的日誌記錄功能。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronPDF

    PM > Install-Package IronPdf

  2. 複製並運行這段程式碼。

    IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.Custom;
    IronSoftware.Logger.CustomLogger = new MyCustomLogger();
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronPDF,免費試用!
    arrow pointer


自訂日誌範例

若要使用自訂日誌記錄功能,請將LoggingMode屬性變更為LoggingModes.Custom 。 之後,將CustomLogger屬性指派給您建立的自訂日誌記錄器類別。

:path=/static-assets/pdf/content-code-examples/how-to/custom-logging-custom-logging.cs
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.Custom;
IronSoftware.Logger.CustomLogger = new CustomLoggerClass("logging");
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.Custom
IronSoftware.Logger.CustomLogger = New CustomLoggerClass("logging")
$vbLabelText   $csharpLabel

IronPdf 日誌將會導向至自訂日誌記錄器物件。 這些訊息將與 IronPdf 日誌記錄器中的訊息完全相同; 它們將直接轉發給自訂日誌記錄器。 自訂日誌記錄器如何管理訊息將由自訂日誌記錄器設計者決定。 讓我們以下面的自訂日誌記錄器類別為例。

:path=/static-assets/pdf/content-code-examples/how-to/custom-logging-custom-logging-class.cs
public class CustomLoggerClass : ILogger
{
    private readonly string categoryName;

    public CustomLoggerClass(string categoryName)
    {
        this.categoryName = categoryName;
    }

    public IDisposable BeginScope<TState>(TState state)
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        if (!IsEnabled(logLevel))
        {
            return;
        }

        // Implement your custom logging logic here.
        string logMessage = formatter(state, exception);

        // You can use 'logLevel', 'eventId', 'categoryName', and 'logMessage' to log the message as needed.
        // For example, you can write it to a file, console, or another destination.

        // Example: Writing to the console
        Console.WriteLine($"[{logLevel}] [{categoryName}] - {logMessage}");
    }
}
Public Class CustomLoggerClass
	Implements ILogger

	Private ReadOnly categoryName As String

	Public Sub New(ByVal categoryName As String)
		Me.categoryName = categoryName
	End Sub

	Public Function BeginScope(Of TState)(ByVal state As TState) As IDisposable
		Return Nothing
	End Function

	Public Function IsEnabled(ByVal logLevel As LogLevel) As Boolean
		Return True
	End Function

	Public Sub Log(Of TState)(ByVal logLevel As LogLevel, ByVal eventId As EventId, ByVal state As TState, ByVal exception As Exception, ByVal formatter As Func(Of TState, Exception, String))
		If Not IsEnabled(logLevel) Then
			Return
		End If

		' Implement your custom logging logic here.
		Dim logMessage As String = formatter(state, exception)

		' You can use 'logLevel', 'eventId', 'categoryName', and 'logMessage' to log the message as needed.
		' For example, you can write it to a file, console, or another destination.

		' Example: Writing to the console
		Console.WriteLine($"[{logLevel}] [{categoryName}] - {logMessage}")
	End Sub
End Class
$vbLabelText   $csharpLabel

在這種情況下,我在日誌訊息前面添加了附加資訊。

控制台視窗

準備好看看您還能做些什麼嗎? 請造訪我們的教學頁面:附加功能

常見問題解答

如何在 C# 中實現自訂記錄?

您可以使用 IronPDF 在 C# 中實現自訂日誌。首先,從 NuGet 下載 IronPdf,然後將 LoggingMode 屬性設定為 LoggingModes.Custom,並將您的自訂記錄器物件指定給 CustomLogger 屬性。此設定可確保所有日誌訊息都會被導向到您的自訂記錄器。

開始使用 IronPDF 自訂日誌需要哪些步驟?

若要使用 IronPDF 開始自訂記錄,請從 NuGet 下載函式庫,將 LoggingMode 設定為 LoggingModes.Custom,指定自訂記錄器至 CustomLogger 屬性,並確保所有記錄訊息都會轉發至您的記錄器進行輸出。

IronPDF 中 CustomLogger 屬性的作用是什麼?

IronPDF 中的 CustomLogger 屬性允許您指定自訂記錄器物件。此功能會將所有日誌訊息轉寄給自訂記錄器,讓您可以根據應用程式的需求,管理記錄和顯示日誌的方式。

您能否提供一個在 C# 中設定自訂記錄器的詳細範例?

當然!要在 C# 中設定自訂記錄器,您需要實作 IronPDF 的 ILogger 介面。定義方法如 LogInformation, LogWarning, 和 LogError 來處理各種日誌訊息,然後將您的自訂日誌器指定給 CustomLogger 屬性。

為什麼要在 C# 應用程式中使用自訂記錄?

在 C# 應用程式中,自訂記錄是有益的,因為它可讓您建立符合特定需求的記錄系統。此自訂功能可透過提供詳細且相關的日誌資訊,強化監控、除錯及維護功能。

使用 IronPDF 自訂日誌有哪些好處?

在 IronPDF 中使用自定義日誌可以靈活地管理日誌訊息。它允許您根據應用程式的需求定制日誌,確保您以最有效的方式捕捉和處理日誌訊息。

ILogger 介面如何支援自訂記錄?

ILogger 介面透過提供您必須在自訂記錄器中實作的結構,來支援自訂記錄。它包含處理不同記錄訊息類型的方法,確保全面的記錄功能。

在 IronPDF 中使用自訂記錄器可以記錄哪些類型的訊息?

IronPDF 中的自訂記錄器可處理各種記錄訊息,包括資訊、警告和錯誤訊息。這種全面的日誌功能可確保您擁有應用程式作業的詳細記錄。

IronPDF 的自定義日誌在 .NET 10 中是否完全支援?

是的,IronPDF 與 .NET 10 完全相容,包括其自訂記錄功能。您可以在 .NET 10 專案中使用 LoggingModes.Custom 並透過 CustomLogger 指定自訂記錄器,而無需任何額外的變通。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 16,493,056 | Version: 2025.11 剛發表