如何使用C#实现自定义日志记录

How to use Custom Logging in C#

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

自定义日志是指实施一个针对特定应用程序或系统的需求和要求量身定制的日志系统。 涉及创建和使用日志文件以记录软件运行期间生成的信息、事件和消息。

快速入门:使用 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 接口。定义 LogInformation, LogWarningLogError 等方法以处理各种日志消息,然后将您的自定义记录器分配给 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,154,058 | 版本: 2025.11 刚刚发布