You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin MvvmCross配置NLog作为IMvxLogProvider无效,求排查

Troubleshooting Your NLog Configuration for Xamarin Android/iOS

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.Config NuGet is the correct starting point
  • Overriding GetDefaultLogProviderType() in Android's Setup.cs to use MvxLogProviderType.NLog aligns perfectly with MvvmCross's logging integration
  • Fetching the IMvxLog instance 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

  1. Run your app and trigger the _logger.Debug() call
  2. Check Logcat for the console target output (search for your log message or logger name)
  3. Navigate to the log file path you set to confirm the file is created and populated
  4. Check the nlog-internal.log file for any errors (like "Could not find configuration file" or "Permission denied")

内容的提问来源于stack exchange,提问作者ibocon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:58:09