带有 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.config
放在我的 android-project 的资产文件夹中。
- 我将 this NuGet 添加到我的 android 项目中。
- 我将构建操作更改为
embedded resource
。
- 我确保对设备的外部存储具有写入权限。
- 我正在以编程方式加载 NLog 配置,例如 this 并且
nlogConfigFile != null
是正确的,所以我假设找到了 NLog.config
。
- 如您所见,我启用了从 NLog 抛出异常,但没有抛出任何异常...
- ...但是内部日志记录也不起作用
- 我遵循了 troubleshooting by 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 ressource 或 android资产.
-------------------- 第二次更新 ---------- --------------
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
。
我在我的 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.config
放在我的 android-project 的资产文件夹中。 - 我将 this NuGet 添加到我的 android 项目中。
- 我将构建操作更改为
embedded resource
。 - 我确保对设备的外部存储具有写入权限。
- 我正在以编程方式加载 NLog 配置,例如 this 并且
nlogConfigFile != null
是正确的,所以我假设找到了NLog.config
。 - 如您所见,我启用了从 NLog 抛出异常,但没有抛出任何异常...
- ...但是内部日志记录也不起作用
- 我遵循了 troubleshooting by 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 ressource 或 android资产.
-------------------- 第二次更新 ---------- --------------
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
。