Skip to content
 
 

Repository files navigation

NLog.Targets.MauiLog

Version AppVeyor

NLog Target for debugging on MAUI / Xamarin Mobile Platforms using the native logging systems:

  • AndroidAndroid.Util.Log → view in Logcat
  • iOS / macOSOSLog → view in Xcode Console or Console.app
  • Other platformsSystem.Diagnostics.Debugger.Log → view in the debugger output

1. Install NLog

Install the following NuGet packages:

dotnet package add NLog.Extensions.Logging
dotnet package add NLog.Targets.MauiLog

Use the package versions appropriate for your .NET version (Use ver. 10 for NET10, and ver. 8 for NET8 etc.).

2. Configure NLog

NLog integrates with the built-in Microsoft.Extensions.Logging infrastructure. Application code continues to use ILogger<T>, while NLog handles log routing, formatting, and writing to configured targets.

Configure NLog in MauiProgram.cs:

using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();

    builder.Logging.ClearProviders();

    // Register NLog MauiLog extension and setup as output target
    NLog.LogManager.Setup()
        .RegisterMauiLog()
        .LoadConfiguration(config => config
            .ForLogger()
            .FilterMinLevel(NLog.LogLevel.Info)
            .WriteToMauiLog());

    // Register NLog as Microsoft.Extensions.Logging provider
    builder.Logging.AddNLog();

    builder.UseMauiApp<App>();

    return builder.Build();
}

3. Start logging

NLog is now configured and receives log events from Microsoft.Extensions.Logging. Application code can continue using ILogger<T> as usual.

See also Logging Unhandled Exceptions

Alternative configuration

The example above uses the Fluent Configuration API. NLog also supports using configuration files like NLog.config or appsettings.json.

When switching from the Fluent Configuration API to loading a aconfiguration file, remove the LoadConfiguration(...) call from the setup.

When using configuration files, explicitly register the MauiLog extension:

NLog.LogManager.Setup()
    .RegisterMauiLog();

This explicit registration is important when using trimming or Native AOT. If an NLog extension is only referenced through a configuration file, the linker can trim away the extension type, causing NLog logging to fail at runtime.

NLog.config

Example NLog.config file:

<nlog throwConfigExceptions="true">
  <targets>
    <target name="mauilog" type="MauiLog" />
  </targets>
  <rules>
    <logger name="*" minLevel="Info" writeTo="mauilog" />
  </rules>
</nlog>

Make sure to add NLog.config file as an Embedded resource in the application project (Build Action = Embedded resource).

Load the embedded configuration during application startup:

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

appsettings.json

NLog AddNLog() loads the "NLog" section when present, with environment overrides.

Example appsettings.json file:

{
  "NLog": {
    "throwConfigExceptions": true,
    "targets": {
      "mauilog": {
        "type": "MauiLog"
      }
    },
    "rules": [
      {
        "logger": "*",
        "minLevel": "Info",
        "writeTo": "mauilog"
      }
    ]
  }
}

See also NLog configuration with appsettings.json

About

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

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages