How to use Custom Logging in C#

How to use Custom Logging in C#

Custom logging in C# allows you to implement a tailored logging system that captures application-specific events and messages. With IronPDF, you can redirect all PDF processing logs to your custom logger by setting LoggingMode to Custom and assigning your logger implementation. This feature enables you to integrate IronPDF's detailed logging output with your existing logging infrastructure, whether you're using NLog, Serilog, log4net, or a custom solution.

Custom logging is valuable when you need to centralize logs from multiple components, apply custom formatting, filter by severity levels, or route logs to specific destinations such as files, databases, or monitoring services. By implementing custom logging with IronPDF, you gain complete control over how PDF generation and processing events are tracked in your application.

Quickstart: Implement Custom Logging with IronPDF

Start with custom logging in C# using IronPDF. This guide shows how to set the LoggingMode to Custom and link it with your logger class. Direct IronPDF messages to your custom logger to monitor and manage application logs. The setup is straightforward and enhances your application's logging capabilities. For a comprehensive overview of IronPDF's features, see our Quickstart Guide.

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


How Do I Implement a Custom Logging Example?

To use custom logging, change the LoggingMode property to LoggingModes.Custom. Then assign the CustomLogger property to your custom logger class. This provides flexibility for integrating with various logging frameworks and maintains consistent logging patterns across your application.

The implementation requires minimal setup. Once configured, all IronPDF operations—including HTML to PDF conversion, PDF manipulation, and rendering—automatically send their log messages through your custom logger. This integration ensures you capture critical information about PDF processing operations.

Why Should I Use Custom Logging with IronPDF?

: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");
$vbLabelText   $csharpLabel

IronPDF logs will be directed to the custom logger object. The messages remain identical to those in the IronPDF logger; they are relayed to the custom logger. The custom logger implementation determines how to handle the messages. Consider the following custom logger class example.

Custom logging offers several advantages for production applications:

  • Centralized Log Management: Integrate IronPDF logs with your existing logging infrastructure
  • Enhanced Debugging: Capture detailed information about PDF operations for troubleshooting
  • Performance Monitoring: Track rendering times and resource usage
  • Error Tracking: Automatically capture and report PDF generation failures
  • Compliance: Maintain audit trails for document generation in regulated industries

For applications requiring asynchronous processing, custom logging helps track concurrent PDF operations and identify potential bottlenecks.

What Does a Custom Logger Class Look Like?

: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}");
    }
}
$vbLabelText   $csharpLabel

This implementation follows the standard Microsoft.Extensions.Logging.ILogger interface, making it compatible with the .NET logging ecosystem. The logger class includes:

  • Category Name: Helps identify the source of log messages
  • Log Level Filtering: Control which severity levels to capture
  • Flexible Output: Route logs to any destination (console, file, database, etc.)
  • Exception Handling: Properly format and log exception details
  • State Management: Support for structured logging with BeginScope

For advanced scenarios, explore rendering options that affect logging output, especially when dealing with complex HTML conversions or when working with the Chrome rendering engine.

How Does Custom Logging Output Look?

In this example, log messages are prefixed with additional information.

Visual Studio debugger console showing IronPdf deployment logs with Chrome client initialization and library loading

The console output demonstrates how custom logging provides detailed insights into IronPDF's internal operations. You can see:

  • Chrome client initialization steps
  • Deployment candidate creation
  • Native library loading sequences
  • Successful component deployment confirmations

This detail level is invaluable when debugging PDF generation issues or optimizing performance. The structured format makes it easy to parse logs programmatically or integrate with log analysis tools.

Best Practices for Custom Logging Implementation

When implementing custom logging with IronPDF, consider these best practices:

  1. Use Appropriate Log Levels: Configure your logger to capture the right detail level without overwhelming log storage
  2. Include Contextual Information: Add request IDs, user information, or transaction details to correlate PDF operations
  3. Handle Sensitive Data: Ensure your logger doesn't expose confidential information from PDF content
  4. Monitor Performance: Track PDF generation times to identify optimization opportunities
  5. Set Up Alerts: Configure notifications for critical errors or performance degradation

For production deployments, especially when using license keys, proper logging ensures smooth operations and quick issue resolution.

Troubleshooting Common Logging Issues

If you encounter problems with custom logging:

  • Verify your logger implements the ILogger interface correctly
  • Check that LoggingMode is set before any IronPDF operations
  • Ensure your logger is thread-safe for concurrent operations
  • Review log permissions if writing to files or external systems

For additional troubleshooting resources, consult our troubleshooting guide or explore the API Reference for detailed documentation on logging properties and methods.

Ready to see what else you can do? Check out our tutorial page here: Additional Features

Frequently Asked Questions

What is custom logging in C# and how does it work with PDF processing?

Custom logging in C# with IronPDF allows you to implement a tailored logging system that captures PDF processing events and messages. By setting LoggingMode to Custom and assigning your logger implementation, IronPDF redirects all PDF generation and manipulation logs to your custom logger, enabling integration with existing logging infrastructure like NLog, Serilog, or log4net.

How do I enable custom logging for PDF operations?

To enable custom logging with IronPDF, set IronSoftware.Logger.LoggingMode to LoggingModes.Custom and assign your custom logger class to IronSoftware.Logger.CustomLogger. Once configured, all IronPDF operations including HTML to PDF conversion and PDF manipulation will automatically send their log messages through your custom logger.

What are the benefits of implementing custom logging for PDF generation?

Custom logging with IronPDF provides complete control over how PDF processing events are tracked. It enables you to centralize logs from multiple components, apply custom formatting, filter by severity levels, and route logs to specific destinations like files, databases, or monitoring services, enhancing your application's logging capabilities.

Can I integrate PDF processing logs with existing logging frameworks?

Yes, IronPDF's custom logging feature seamlessly integrates with popular logging frameworks including NLog, Serilog, log4net, or any custom logging solution. This ensures consistent logging patterns across your application while capturing critical information about PDF processing operations.

What is the minimal workflow to implement custom logging?

The minimal workflow involves 5 steps: Download IronPDF from NuGet, set LoggingMode to LoggingModes.Custom, assign CustomLogger to your custom logger object, and all log messages will be forwarded to your custom logger. Finally, output the log messages to view the logs from IronPDF operations.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Ready to Get Started?
Nuget Downloads 16,901,161 | Version: 2025.12 just released