Skip to content

NLog MauiLog Target for writing to Apple Unified Logging OSLog / Android.Util.Log for LogCat

License

Notifications You must be signed in to change notification settings

snakefoot/NLog.Targets.MauiLog

 
 

Repository files navigation

NLog.Targets.MauiLog

NLog Target for debugging on MAUI / Xamarin Mobile Platforms:

  • Apple iOS / MacOS - Unified Logging OSLog (replacement of print and NSLog)
  • Android - Android.Util.Log / LogCat
  • NetStandard - System.Diagnostics.Debugger.Log

Version AppVeyor

How to setup NLog in MAUI

  1. Install the NLog packages

    • Install-Package NLog.Targets.MauiLog
    • Install-Package NLog.Extensions.Logging

    or in your csproj (Use ver. 8 for NET8, and ver. 7 for NET7 etc.):

    <PackageReference Include="NLog.Targets.MauiLog" Version="8.*" />
    <PackageReference Include="NLog.Extensions.Logging" Version="5.*" />
  2. Add NLog to the MauiApp

    Update MauiProgram.cs to include NLog as Logging Provider:

    var builder = MauiApp.CreateBuilder();
    
    // Add NLog for Logging
    builder.Logging.ClearProviders();
    builder.Logging.AddNLog();

    If getting compiler errors with unknown methods, then update using-section:

    using Microsoft.Extensions.Logging;
    using NLog;
    using NLog.Extensions.Logging;
  3. Load NLog configuration for logging

    Add the NLog.config-file into the Application-project as assembly-resource (Build Action = embedded resource), and load like this:

    NLog.LogManager.Setup().RegisterMauiLog()
        .LoadConfigurationFromAssemblyResource(typeof(App).Assembly);

    Alternative setup NLog configuration using fluent-API:

    var logger = NLog.LogManager.Setup().RegisterMauiLog()
                     .LoadConfiguration(c => c.ForLogger().FilterMinLevel(NLog.LogLevel.Debug).WriteToMauiLog())
                     .GetCurrentClassLogger();

Configuration options for MAUI Log Target

  • Layout - LogEvent message layout
  • Category - LogEvent category layout (optional)

Example NLog.config-file:

<nlog>
<extensions>
    <add assembly="NLog.Targets.MauiLog" />
</extensions>
<targets>
    <target name="mauilog" type="MauiLog" />
</targets>
<rules>
    <logger name="*" minLevel="Info" writeTo="mauilog" />
</rules>
</nlog>

See also Logging Unhandled Exceptions

About

NLog MauiLog Target for writing to Apple Unified Logging OSLog / Android.Util.Log for LogCat

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 98.6%
  • PowerShell 1.4%