【发布时间】:2012-05-11 11:50:33
【问题描述】:
我正在尝试将日志记录添加到使用 Windows Mobile 6.1 的移动设备上运行的应用程序。 .NET Compact 框架 3.5。使用 NLog。
我安装了适当版本的 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">
<targets>
<target name="logfile" xsi:type="File" fileName=".\Neolant.ASRM.Terminal.log" layout="${longdate}|${level}|${message}|${exception}" autoFlush="true"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>
这是我使用的测试代码:
public static void Main()
{
try
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.Info("test test test.");
try
{
throw new Exception("Unexpected!");
}
catch (Exception e)
{
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.WarnException("An exception occured.", e);
}
throw new Exception("Suddenly!");
}
finally
{
NLog.LogManager.Flush();
}
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.FatalException("Application closed due to exception.", unhandledExceptionEventArgs.ExceptionObject as Exception);
NLog.LogManager.Flush();
}
【问题讨论】:
-
您是否尝试过不同的文件名/路径?例如
fileName="Neolant.ASRM.Terminal.log"没有 `.\` ? Nlog.Config 在 app 目录中?此外,您可以打开Nlog's internal logging 以获取有关您的问题的更多信息。 -
我尝试了带和不带'.\'的文件名,结果相似(也就是说,没有)。 NLog.config 被部署到应用程序目录中。现在将尝试内部日志记录。
-
按照您提供的链接,我偶然发现了解决方案。非常感谢。
-
我很高兴 :) 那么请将您的解决方案发布为以后访问者的答案。
-
这里是解决 NLog 问题的更新链接,供我们这些通过 Google 搜索来到这里的人...github.com/NLog/NLog/wiki/Logging-troubleshooting
标签: c# compact-framework nlog