【问题标题】:NLog does not create a log fileNLog 不创建日志文件
【发布时间】: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


【解决方案1】:

我遇到了这个问题,原来我的日志文件没有被复制到我的构建目录。 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 时,可能会发生这种情况。

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

【讨论】:

  • 谢谢。 “请勿复制”设置是问题所在。
【解决方案2】:

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

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

【讨论】:

  • 嗯,很奇怪。这为我修复了它,但我有另一个应用程序不需要指定(并且输出仍然有效)
  • 如果您找出原因,请发布。我认为了解 NLog 日志文件放置可以依赖的内容会很有帮助
  • 那个文档太可怕了。你能在你的回答中举个例子吗?
  • 寻找毛里西奥的另一个答案,他已经慷慨地做到了
【解决方案3】:

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

<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>

取自这里using AppData location in NLog

【讨论】:

    【解决方案4】:

    来自nlog troubleshooting guide

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

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

    【讨论】:

    • 谢谢,我总是忘记设置nlog.config文件复制。我认为如果找不到nlog.config文件,nlog应该抛出异常。
    【解决方案5】:

    我的问题是权限相关的,日志文件需要允许进程写入它,没有写入权限你将没有文件。

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

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

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

    【讨论】:

      【解决方案6】:

      来自 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>
      

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

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

      【讨论】:

      • 粗体文本“'bin'目录”对我来说很关键。我没有意识到它在里面。一定是错过了文档。非常感谢。
      • 这也是我的问题,我在做 ASP.NET,{$basedir}\bin\Debug\net5.0(Rider Explorer 视图中未显示文件夹)
      【解决方案7】:

      在我的情况下,我在定义规则后错过了规则

       <rules>
          <logger name="*" minlevel="Trace" writeTo="logfile" />
          <!-- add your logging rules here -->
      
          <!--
          Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
          <logger name="*" minlevel="Debug" writeTo="f" />
          -->
        </rules>
      

      【讨论】:

      • 聚会迟到了,但这个答案对我来说是解决方案。
      【解决方案8】:

      在这个问题上花了很多时间。这是我的问题。我正在使用安装项目来安装带有 MSI 的 Windows 服务。我必须手动将 NLog.config 添加到安装程序的输出中,以确保将其复制到服务的安装目录中

      【讨论】:

        【解决方案9】:

        我也遇到了同样的问题,终于解决了。我有一个 Web 应用程序,我想在其中实现 NLog。请找到以下步骤来实现 NLog。

        第 1 步:- 转到 NuGet 包管理器并安装以下包。

        第 2 步:- 打开 Web.config 并添加以下行

        <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
        </configSections>
         <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="false"
          internalLogLevel="Off" internalLogFile="D:\projects\NlogWeb\nlog-internal.log">
        <targets>
          <target name="console" xsi:type="ColoredConsole" layout="${message}" />
          <!--Write logs to File-->
          <target name="file" xsi:type="File" fileName="D:\projects\NlogWeb\ErrorLogFile.log" layout="--------------------- ${level}(${longdate})${machinename}-------------------- ${newline}
        Exception Type:${exception:format=Type}${newline} 
        Exception Message:${exception:format=Message}${newline}  
        Stack Trace:${exception:format=Stack Trace}${newline}  
        Additional Info:${message}${newline}" >
        </target>  
        </targets>   
        <rules>
        
          <logger name="*" minlevel="trace" writeTo="file" />
        </rules>
        </nlog>
        

        第 3 步:- 现在是在 .cs 文件中调用的最后一个配置。

        using NLog;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        
        namespace NlogWeb
        {
        public partial class Home : System.Web.UI.Page
        {
            private static Logger logger = LogManager.GetCurrentClassLogger();
            protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    throw new Exception("Divide By Zero Exception", new DivideByZeroException());
                }
                catch (Exception ex)
                {                 
                    logger.Error(ex.Message);
                }
            }
          }
        }
        

        我们已经完成了。现在执行您的代码并享受日志记录。

        【讨论】:

          【解决方案10】:

          要解决此问题,我必须以管理员模式运行我的应用程序。我怀疑 windows 有一个更新突然阻止 exe 创建(日志)文件,因为 exe 总是可以在没有管理员权限的情况下预先登录。

          【讨论】:

            【解决方案11】:

            在我的例子中,我不得不在代码中手动加载 NLog.config 文件,因为它不是自动找到的。必须在生成日志之前加载配置。

            LogManager.LoadConfiguration(@"D:\doe\ConsoleApp2\ConsoleApp2\NLog.config");
            

            之后我得到了日志文件和控制台输出。

            【讨论】:

            【解决方案12】:

            在我的情况下,WebAPI 应用程序,我通过将修改权限授予 IIS_IUSRS 网站文件夹 C:\inetpub\wwwroot\my_website 的修改权限来解决问题

            【讨论】:

              【解决方案13】:

              为了简单的故障排除,启动 VisualStudio 以管理员身份运行。这将有助于整理在调试时创建日志文件的权限。

              还可以在每个目标部分中使用 createDirs=true 以在目标部分提供的文件路径中自动创建缺少的文件夹。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2020-11-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多