Xamarin MvvmCross配置NLog作为IMvxLogProvider无效,求排查
Let's walk through your setup and configuration to spot potential issues, plus share practical tweaks to get your logs flowing properly:
First, Your Core Setup Looks Solid
Let's confirm the basics you've got right:
- Installing
NLog.ConfigNuGet is the correct starting point - Overriding
GetDefaultLogProviderType()in Android'sSetup.csto useMvxLogProviderType.NLogaligns perfectly with MvvmCross's logging integration - Fetching the
IMvxLoginstance via the IoC container is the proper way to get a logger for your class - Setting
NLog.config's Build Action to Embedded Resource is necessary for NLog to load it from your app's assembly
Potential Issues in Your NLog.config
Your config is syntactically valid, but there are platform-specific tweaks needed for Android/iOS:
1. Invalid Internal Log Path for Android
Your internalLogFile is set to c:\temp\nlog-internal.log—that's a Windows-only path, which won't work on Android. NLog uses this file to log its own internal errors (like failed config loading or write permissions), so fixing this is critical for troubleshooting.
Update it to an Android-accessible path, and add debug-level logging for more detail:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogLevel="Debug" internalLogFile="${specialfolder:folder=MyDocuments}/nlog-internal.log">
2. File Target Path Accessibility
Your FileTarget uses ${specialfolder:folder=MyDocuments}, which on Android points to your app's private internal storage (/data/data/[your-package-name]/files). This path is only accessible by your app, so you'll need ADB or a rooted device file manager to view logs here.
If you want easier access to logs, switch to your app's external storage directory (no special permissions needed for Android 10+):
<target xsi:type="File" name="FileTarget" fileName="${specialfolder:folder=ExternalStorage}/Android/data/${processname}/logs/${shortdate}.log" layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
This saves logs to Android/data/[your-package-name]/logs/ on the device's external storage, which you can access directly via the device's file manager.
3. ColoredConsole Target on Mobile
The ColoredConsole target works on Android, but it outputs to Logcat instead of a physical console. The color formatting won't show up in Logcat, but the log content will still be visible—this is totally fine, just be aware you won't see colored text.
Extra Steps to Ensure Configuration Loads
Even with the Build Action set to Embedded Resource, sometimes NLog needs a nudge to load the config from your Android assembly. Add this code to your Setup.cs's InitializeLastChance() method:
protected override void InitializeLastChance() { base.InitializeLastChance(); // Load NLog config from embedded resource var assembly = typeof(Setup).Assembly; using var configStream = assembly.GetManifestResourceStream("YourAndroidProjectNamespace.NLog.config"); if (configStream != null) { LogManager.Configuration = new XmlLoggingConfiguration(System.Xml.XmlReader.Create(configStream), null); } }
Replace YourAndroidProjectNamespace with your actual Android project's root namespace (this matches the embedded resource's full name).
Quick Validation Test
- Run your app and trigger the
_logger.Debug()call - Check Logcat for the console target output (search for your log message or logger name)
- Navigate to the log file path you set to confirm the file is created and populated
- Check the
nlog-internal.logfile for any errors (like "Could not find configuration file" or "Permission denied")
内容的提问来源于stack exchange,提问作者ibocon

