NLog不会创建日志文件

Srv*_*v19 31 c# compact-framework nlog

我正在尝试将日志记录添加到使用Windows Mobile 6.1在移动设备上运行的应用程序. .NETCompact框架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>
Run Code Online (Sandbox Code Playgroud)

这是我使用的测试代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

Mal*_*son 25

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

NLog无法找到配置文件.当NLog.config文件配置为Build Action = None或Copy to Output Directory =不在Visual Studio中复制时,可能会发生这种情况.

设置构建操作=内容和"复制到输出目录=如果更新则复制以修复此问题"

  • 谢谢."不要复制"设置是问题所在. (3认同)

Srv*_*v19 21

正在创建日志文件 - 但不在应用程序目录中.

使用$ {basedir}布局渲染器作为文件名的一部分被证明是一种解决方案.

  • 谢谢.固定它. (2认同)
  • 如果你知道是什么原因,请发布它。我认为了解 NLog 日志文件放置可以依赖的内容会很有帮助 (2认同)
  • 那份文件太可怕了.你能在答案中加上一个例子吗? (2认同)
  • 寻找 Mauricio 的另一个答案,他已经慷慨地做到了这一点 (2认同)

Mau*_*rez 15

如果标记为答案的响应不是那么清楚,您可以查看示例

<targets>
  <target xsi:type="Console" name="console" 
    layout="${longdate}|${level}|${message}" />
  <target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
          layout="${longdate}
          Trace: ${stacktrace} 
          ${message}" />
  <target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
          layout="${shortdate} | ${message}" />
</targets>
Run Code Online (Sandbox Code Playgroud)

从这里使用NLog中的AppData位置


Ale*_*ens 9

我的问题与权限相关,日志文件需要允许进程写入它,如果没有写入权限,您将无法获得文件。

以下是修复 IIS 中网站的方法:

  1. 右键单击 Windows 资源管理器中的文件夹并选择属性
  2. 选择安全选项卡
  3. 点击编辑
  4. 点击添加
  5. 在文本框中输入“ IIS AppPool\YourAppPoolName,将 YourAppPoolName 替换为您的网站运行的应用程序池的实际名称
  6. 单击检查姓名
  7. 单击“确定”

安全脚注:从安全角度来看,最佳实践是使用 ApplicationPoolIdentity,因为它是动态创建的非特权帐户。更多阅读此处:https ://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities


Chr*_*row 5

从 nlog 故障排除指南:

https://github.com/NLog/NLog/wiki/Logging-troubleshooting

如果您知道您的配置文件肯定会被找到,请暂时将 NLog.config 文件的部分替换为以下内容并尝试。

此规则将匹配您创建的任何记录器,如果它有效,它将在“基本目录”中放置一个 log.txt 文件 - 这是您的测试实例的“bin”目录,例如,如果您正在调试中运行模式,您将在 bin > debug 文件夹中看到 log.txt。(这在故障排除指南中没有很清楚地解释)。

如果这有效,那么您就知道问题出在您的规则上:

<nlog throwExceptions="true">
  <targets>
    <target name="file" type="File" fileName="${basedir}/log.txt" />
  </targets>
  <rules>
    <logger name="*" minLevel="Trace" writeTo="file" />
  </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

我发现只有 name="file" 对目标有效 - 其他值没有

如上所述添加 throwExceptions="true" 还可以确保您在调试时获得有用的错误消息。


Pre*_*dri 5

从nlog故障排除指南

请检查Nlog.config文件属性:复制到输出目录应始终复制

请查看图片链接https://i.stack.imgur.com/AlUG5.png

  • 谢谢,我总是忘记设置 nlog.config 文件副本。我认为如果 nlog 找不到 nlog.config 文件,它应该抛出异常。 (2认同)