Logging abstraction for Xamarin (with NLog implementation)
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
build
src
tests/Logging.Tests
tools
.appveyor.yml
.gitignore
CHANGELOG
GitVersion.yml
LICENSE.md
README.md
WD.Logging.Sonar.sln
WD.Logging.sln
build.cake
build.ps1
build.sh
cake.config

README.md

WebDucer library for logging in Xamarin.Forms (Prism)

A generic abstraction for the logger to be used with dependency injection in Xamarin.Forms (e.g. with Prism.Forms).

States

Service Last Develop Master
AppVeyor Build status last Build status develop Build status master
SonarCube coverage SonarQube Coverage SonarQube Coverage
SonarCube technical debt SonarQube Technical Debt SonarQube Technical Debt
SonarCube Quality Gate SonarQube Quality Gate SonarQube Quality Gate
NuGet NuGet NuGet Pre Release NuGet

Services

  • ILogger<> / ILogger - Abstraction for logger
  • ILoggerConfiguration - Abstraction, to configure the current logger with code

Sample

Registration

...
containerRegistry.RegisterSingleton<ILoggerConfiguration, NLogLoggerConfiguration>();
containerRegistry.RegisterSingleton(typeof(ILogger<>), typeof(NLogLoggerAdapter<>));
...

Configuration

private void InitLogger()
{
    var logConfiguration = Container.Resolve<ILoggerConfiguration>();
    logConfiguration.ApplyConfiguration(options =>
        options
            .WithFile("RaumAkustik.log")
            .WithLevel(LogLevel.Trace)
            .WithMaxSize(new FileSize { SizeType = SizeType.MibiByte, Size = 2 })
            .WithArchiveCount(6)
            .Compress(false)
            .ArchiveOnStart(false)
            .WithLogMessageLayout("${longdate};${logger};${message}")
            .WithFilter("MyAssembly.Namespace.MyClass*")
#if DEBUG
            .AddDebugTarget()
#endif
    );
}

Change configuration

private void SetLogLevel(LogLevel level, string newFilter)
{
    _loggerConfiguration.ChangeConfiguration(options =>
        options.WithLevel(level)
            .WithFilter(newFilter)
    );
}

Usage

...
public class MyClass
{
    private readonly ILogger _looger;

    public MyClass(ILogger<MyClass> logger)
    {
        _logger = logger;
    }
...
    private async Task SomeMethod()
    {
        try {
            _logger.Trace("Start processing");

            var data = await ProcessSomeData();

            _logger.Trace("Finsih processing");
        } catch (Exception ex){
            _logger.Error(ex, "Processing failed for {0}", nameof(ProcessSomeData));
        }
    }
...