Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

WebDucer/WD.Logging

Repository files navigation

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));
        }
    }
...

About

Logging abstraction for Xamarin (with NLog implementation)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published