Skip to content

Benchmarking ways of logging messages with or without arguments

Notifications You must be signed in to change notification settings

aimenux/LoggingBenchDemo

Repository files navigation

.NET

LoggingBenchDemo

Benchmarking ways of logging messages with or without arguments

In this demo, i m using BenchmarkDotNet library in order to benchmark various ways of logging messages with or without arguments.

1️⃣ DefaultLoggerBench : a bench based on default logger provided by microsoft

2️⃣ DefaultLoggerVariantBench : a bench based on a custom wrapper around default logger

3️⃣ SerilogLoggerBench : a bench based on a default logger configured with serilog logger

4️⃣ SerilogLoggerVariantBench : a bench based directly on serilog logger

In order to run benchmarks, type this command in your favorite terminal :

✍️ .\App.exe --filter *DefaultLoggerBench*

✍️ .\App.exe --filter *DefaultLoggerVariantBench*

✍️ .\App.exe --filter *SerilogLoggerBench*

✍️ .\App.exe --filter *SerilogLoggerVariantBench*

|                                   Method | Length |       Mean |        Min |        Max | Rank |  Gen 0 | Allocated |
|----------------------------------------- |------- |-----------:|-----------:|-----------:|-----:|-------:|----------:|
|       NoParamsTraceLoggingUsingIsEnabled |     50 |   8.150 ns |   7.863 ns |   8.918 ns |    1 |      - |         - |
|     WithParamsTraceLoggingUsingIsEnabled |     50 |   9.540 ns |   8.514 ns |  11.915 ns |    2 |      - |         - |
|   NoParamsTraceLoggingUsingLoggerMessage |     50 |   9.575 ns |   9.032 ns |  10.744 ns |    2 |      - |         - |
|          NoParamsTraceLoggingUsingDefine |     50 |  10.493 ns |  10.046 ns |  11.195 ns |    3 |      - |         - |
| WithParamsTraceLoggingUsingLoggerMessage |     50 |  17.202 ns |  15.309 ns |  21.691 ns |    4 |      - |         - |
|        WithParamsTraceLoggingUsingDefine |     50 |  17.657 ns |  16.493 ns |  20.044 ns |    5 |      - |         - |
|                     NoParamsTraceLogging |     50 |  31.430 ns |  30.491 ns |  33.146 ns |    6 |      - |         - |
|                   WithParamsTraceLogging |     50 | 113.599 ns | 100.364 ns | 141.127 ns |    7 | 0.0134 |      56 B |

As you can see, the default logger is not optimized unless you use IsEnabled, Define or LoggerMessage in order to eliminates the issue of object[] allocation and unnecessary calculations.

References :

You are doing .NET logging wrong

High-Performance logging with LoggerMessage

Compile-time logging source generation

Tools : vs22, net 6.0, serilog

About

Benchmarking ways of logging messages with or without arguments

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages