Skip to content

Serilog

Arnel Robles edited this page Sep 28, 2026 · 1 revision

Package 6: Mapsicle.Serilog

Structured logging for mapping calls, with timing and an optional slow-mapping warning.

Basic Setup

using Mapsicle.Serilog;
using Serilog;

var logger = new LoggerConfiguration()
    .WriteTo.Console()
    .MinimumLevel.Debug()
    .CreateLogger();

// Global: every MapWithLogging call writes to this logger
SerilogExtensions.UseSerilog(logger);

UseSerilog also sets Mapper.Logger, so the core's own diagnostics (depth limits, cycles) go to the same logger at Information level.

Map with Logging

var dto = user.MapWithLogging<UserDto>();
// [Mapsicle] Mapped {SourceType} -> {DestType} in {ElapsedMs:F2}ms (cached: {IsCached})

var dtos = users.MapCollectionWithLogging<UserDto>();
// [Mapsicle] Mapped collection of {Count} items to {DestType} in {ElapsedMs:F2}ms

The logger comes from UseSerilog, not from the call. A mapping that throws is logged at Error and rethrown.

Slow Mapping Warnings

Off by default. Turn it on when you call UseSerilog:

SerilogExtensions.UseSerilog(logger, options =>
{
    options.SlowMappingThreshold = TimeSpan.FromMilliseconds(50);
});

var dto = largeObject.MapWithLogging<LargeDto>();
// Over 50 ms, also logs at Warning: [Mapsicle] Slow mapping detected: ...

Scoped Logging for Batch Operations

using (var scope = SerilogExtensions.BeginMappingScope("OrderProcessing"))
{
    foreach (var order in orders)
    {
        order.MapWithLogging<OrderDto>();
        scope.RecordMapping();
    }
}
// On dispose: [Mapsicle] Completed OrderProcessing: {MappingCount} mappings in {ElapsedMs:F2}ms

The scope counts only what you record with RecordMapping() and RecordError(). Mapping calls inside it are not counted or tagged automatically. It logs through the logger UseSerilog set, so call that first.


Clone this wiki locally