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

How to use Custom Logging in C#

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

自訂日誌記錄是指實施一個針對應用程式或系統的特定需求和要求定制的日誌系統。 這涉及創建和使用日誌檔案來記錄軟體在運行過程中生成的信息、事件和消息。

作為標題:2(快速入門: 使用 IronPDF 實現自訂日誌記錄)

使用 IronPDF 在 C# 中快速開始自訂日誌記錄。 本指南將向您展示如何將 LoggingMode 設置為 Custom 並將其鏈接到您的自訂日誌記錄類。 通過將 IronPDF 消息定向到您的自訂日誌記錄,來有效地監控和管理應用程式日誌。 設置很簡單,並允許您增強應用程式的日誌記錄功能以滿足您的獨特需求。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.Custom;
    IronSoftware.Logger.CustomLogger = new MyCustomLogger();
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 從 NuGet 下載 IronPDF
  2. LoggingMode 屬性設置為 LoggingModes.Custom
  3. CustomLogger 屬性分配到您的自訂日誌記錄對象
  4. 所有日誌消息都將轉發到自訂日誌記錄器
  5. 輸出日誌消息以查看日誌


自訂日誌記錄範例

要利用自訂日誌記錄功能,請將 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

在此情況下,我為日誌消息添加了其他信息作為前綴。

class="content-img-align-center">
class="center-image-wrapper"> 控制台窗口

準備看看您還能做哪些其他事情嗎? 查看我們的教程頁面:附加功能

常見問題解答

如何在C#中實作自訂日誌記錄?

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

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

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

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

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

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

當然可以!若要在 C# 中設定自訂日誌記錄器,您需要實作 IronPDF 的ILogger介面。定義諸如LogInformationLogWarningLogError之類的方法來處理各種日誌訊息,然後將您的自訂日誌記錄器指派給CustomLogger屬性。

為什麼我的 C# 應用程式需要使用自訂日誌記錄?

在 C# 應用程式中,自訂日誌記錄非常有用,因為它允許您建立滿足特定需求的日誌系統。這種自訂功能透過提供詳細且相關的日誌信息,增強了監控、調試和維護能力。

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

使用 IronPDF 的自訂日誌記錄功能,可以靈活地管理日誌訊息。您可以根據應用程式的需求自訂日誌記錄,確保以最有效的方式擷取和處理流程中的日誌訊息。

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

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

IronPDF 中的自訂日誌記錄器可以記錄哪些類型的消息?

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

IronPDF 的自訂日誌記錄功能在 .NET 10 中是否完全支援?

是的。 IronPDF 完全相容於 .NET 10,包括其自訂日誌記錄功能。您可以在 .NET 10 專案中使用LoggingModes.Custom並透過CustomLogger指派自訂日誌記錄器,無需任何其他變通方法。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布