Skip to content
Grigory Perepechko edited this page May 30, 2018 · 20 revisions

XLog - Minimalistic logging PCL library for (.NET 4.5+, WP8, UWP, Xamarin iOS, Xamarin Android, Xamarin Mac)

To install PCL library to .net standard project please add following line to your .csproj:

<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10</PackageTargetFallback>

In the last project i participate in, we have all logic in PCL libraries (78 profile). 1.5 years ago there was only one PCL logger – MetroLog. It was partially copied from log4net/NLog and contained too much async/await logic (mostly IO related), which is too expensive for mobile devices, especially if app performs very detailed logging as our app does.

So we outlined few goals for a perfect logging library:

  • Very small in terms of lines of code.
  • Minimal footprint on IOS IPA size
  • Very fast with minimal possible amount of memory allocations
  • No dependencies except for .NET
  • Support of WP8, Xamarin, UWP
  • Support categories
My colleague Oleg Ilyin wrote an implementation that satisfied those goals, which i optimized to reach acceptable performance and uploaded to github.
  • We have a library that has ~300 lines of code according to Visual Studio metrics.
  • NET dll size is ~30kb, IOS binary footprint is less than 70kb for ARMv7+ARM64.
  • Logging one message to file produces 1 memory allocation, which is much less that NLogs 25, and is ~35 times faster in terms of CPU time (FileTarget, Console app sample).
On my machine (SSD, 4.4GHz AMD CPU) 1,000,000 records are written to file in 300ms using XLog and in 11,200ms using NLog

Example of logging:

 private static readonly Logger Log = LogManager.Default.GetLogger("ClassName");
 // ....
 for (int i = 0; i < 100000; i++)
     Log.Info(“Hello world!”);

Example of output:

 16:18:17:439|INFO|1|MainController|Hello world!

Example of init logic

 private static void InitLogger()
 {
     var formatter = new LineFormatter();
     var logConfig = new LogConfig(formatter);
     var target = new SyncFileTarget(@"D:\Log.log");
     logConfig.AddTarget(LogLevel.Trace, LogLevel.Fatal, target);
     LogManager.Init(logConfig);
 }
Clone this wiki locally