【问题标题】:NLog file not writingNLog 文件未写入
【发布时间】:2018-08-16 15:06:43
【问题描述】:

我一直在尝试构建一个使用 NLog 来记录文件的应用程序。代码非常基本,但似乎不起作用。有人有想法么?我也将正确的东西设置为“始终复制”,就像在这个问题中一样。 NLog doen't work on IIS

代码如下。

MAIN(包括 using 语句以表明我实际上正在使用它们)

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;

class Program
{

    static void Main(string[] args)
    {
        var logger = LogManager.GetCurrentClassLogger();
        logger.Debug("xxxx");
    }

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="file" xsi:type="File"
    layout="${longdate} ${logger} ${message}"
    fileName="${basedir}/log/logfile.txt" />
</targets>

<rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
<runtime>
  <assemblyBinding>
    <dependentAssembly>
      <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c"   culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.2.1.0" newVersion="3.2.1.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

</configuration>

【问题讨论】:

  • 日志文件夹是否存在?使用createDirs="true" 自动为文件目标创建目录。如果这不起作用,您可能没有该目录的权限。
  • 您是否尝试过这里的故障排除:github.com/NLog/NLog/wiki/Logging-Troubleshooting
  • 你的 exe 对 ${basedir} 有写权限吗?
  • @AustinDonley 尝试从您的代码中创建一个空文件。提示:如果您的应用程序是从一个特殊文件夹(windows、程序文件等)运行并且没有运行提升权限,则默认情况下它没有写入权限。
  • 很高兴听到您解决了这个问题!将在一分钟内发布。

标签: c# .net nlog


【解决方案1】:

检查您的进程是否对${basedir} 位置具有写入权限。此外,如果您的应用程序从一个特殊文件夹运行,请确保该进程正在运行提升。

【讨论】:

  • 感谢米海的回答
【解决方案2】:

您是否从 GetCurrentClassLogger 调用中获得了有效的记录器?

您是否尝试过打开内部调试,如下所示: https://github.com/NLog/NLog/wiki/Internal-Logging

这应该为您指明未创建日志文件的正确方向

【讨论】:

  • 不是问题,但无论如何都有很好的信息。谢谢
【解决方案3】:

如果您尝试从 Visual Studio 进行调试,请尝试以提升的帐户(以管理员身份运行)运行 VS,然后开始调试。这样 NLog 将有足够的权限来创建日志文件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多