gpt4 book ai didi

c# - NLog 不创建日志文件

转载 作者:IT王子 更新时间:2023-10-29 04:09:09 26 4
gpt4 key购买 nike

我正在尝试将日志记录添加到在装有 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();
}

最佳答案

我遇到这个问题是因为我的日志文件没有被复制到我的构建目录中。 NLog github 页面有答案。 (为了更好的可读性,我对段落进行了一些重新格式化。) https://github.com/NLog/NLog/wiki/Logging-troubleshooting

NLog 找不到配置文件。当 NLog.config 文件配置为 Build Action = None 或 Copy to Output Directory = Do not copy in Visual Studio 时,可能会发生这种情况。

设置 Build Action = Content 和“Copy to Output Directory = Copy if newer to fix this)

关于c# - NLog 不创建日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10550788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com