Skip to content
Grigory Perepechko edited this page Nov 12, 2015 · 20 revisions
  1. XLog
Minimal logging PCL library for (.NET, WP8, UWP, Xamarin iOS, Xamarin Android)

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 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 a little bit and uploaded to github.
  • We have a library that has ~200 lines of code according to Visual Studio metrics.
  • NET dll size is ~20kb, IOS binary footprint is <50kb for ARMv7+ARM64.
  • Logging one message to file produces 5 memory allocations, which is much less that NLog/MetroLog do
1 for log Entry class 2 for DateTime formatting 1 for final output string 1 for char[] during writing to File

Example of logging:

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);
    logConfig.AddTarget(LogLevel.Trace, LogLevel.Fatal, new SyncFileTarget(@”I:\Log.log”));
    LogManager.Init(logConfig);

}

Clone this wiki locally