带有 Xamarin 跨平台应用程序的 NLog 不记录任何内容

NLog with Xamarin cross-plattform application doesn't log anything

我在我的 Xamarin 项目的 Android 部分中像 this 一样设置了 NLog。

我的 NLog.config 看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      throwConfigExceptions="true"
      internalLogLevel ="Trace"
      internalLogFile ="C:\Users\MyUser\intLogFile.txt"
      internalLogToConsole="true"
      internalLogIncludeTimestamp ="true">
     <targets>
        <target name="file" xsi:type="File"
            layout="${longdate} ${logger} ${message}${exception:format=ToString}" 
            fileName="/storage/emulated/0/Download/${shortdate}.log" />
    </targets>
 
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

有人知道为什么 NLog 仍然没有记录任何东西吗?

-------------------- 更新 -------------- --------------

NLog 现在使用 LogService 的基本设置进行记录-class:

 public void Initialize(Assembly assembly, string assemblyName)
    {
       
        var location = $"{assemblyName}.NLog.config";
        var stream = assembly.GetManifestResourceStream(location);
        NLog.LogManager.Configuration = new XmlLoggingConfiguration(XmlReader.Create(stream), null);

        NLog.Common.InternalLogger.LogFile = "C:\Users\MyUser\Documents\internalLog.log";

        NLog.Common.InternalLogger.LogWriter = new StringWriter();

        NLog.Common.InternalLogger.LogLevel = LogLevel.Debug;

    }

但是 内部日志仍然没有 出现。如您所见,我尝试在代码和 NLog.config 中配置 InternalLogger。我尝试将 NLog.config 放入 assets-文件夹和主 android-项目中,并将构建操作分别设置为 embedded ressourceandroid资产.

-------------------- 第二次更新 ---------- --------------

InternalLogger 现在正在我的 Android 设备上工作。 在 this tutorial 中,内部文件被记录到 c://temp/...,我假设这是 Windows 上的路径。但是我在调​​试过程中仍然无法实现这一点。这可能吗?

遵循一些帮助我解决问题的其他故障排除提示:

  • 如果您使用的是真正的 Andriod-device 而不是模拟器,请拔下 调试连接并重新连接。 Windows-File-Explorer
    有时不更新设备文件夹中的文件 if
    它没有重新连接。

  • 代码中的基本初始化就足够了:

       public void Initialize(Assembly assembly, string assemblyName)
       {
    
               var location = $"{assemblyName}.NLog.config";
               var stream = assembly.GetManifestResourceStream(location);
               NLog.LogManager.Configuration = new XmlLoggingConfiguration(XmlReader.Create(stream), null);
       }
    
  • 您可以将 NLog.config 作为 embedded resource 放入 android 项目的主文件夹中。如果不将其作为 android asset 放入 assets 文件夹中。

  • 如果仍然没有记录,请尝试设置配置 programmatically

  • 还要确保 .config 中的 minlevel 低于您正在使用的 loglevel