How to use Custom Logging in C#

by Chaknith Bin

Custom logging refers to the practice of implementing a logging system tailored to the specific needs and requirements of an application or system. It involves creating and using log files to record information, events, and messages generated by the software during its operation.

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf

Download DLL

Download DLL

Manually install into your project

Custom Logging Example

To utilize the custom logging feature, change the LoggingMode property to LoggingModes.Custom. Afterward, assign the CustomLogger property to the custom logger class that you have created.

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")
VB   C#

IronPdf logs will be directed to the custom logger object. The messages will remain identical to those in the IronPdf logger; they will simply be relayed to the custom logger. How the custom logger manages the messages will be determined by the custom logger designer. Let's use the following custom logger class as an example.

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))

        // 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
		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
VB   C#

In this case, I have prefixed the log messages with additional information.

Console window

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.