NLog Target for debugging on MAUI / Xamarin Mobile Platforms:
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="6.*" />
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;
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();
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4