c# - 如何使用 NLog 在当前目录中创建文本文件?

标签 c# console nlog

我是第一次使用 Nlog。我的目标是只写入一个文本文件。

在 main.c 中我有

class Program
{
    private static Logger logger = LogManager.GetCurrentClassLogger();

static void Main(string[] args)
{
        logger.Trace("Sample trace message");
        logger.Debug("Sample debug message");
        logger.Info("Sample informational message");
        logger.Warn("Sample warning message");
        logger.Error("Sample error message");
        logger.Fatal("Sample fatal error message");
    }
}

我的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="file.txt" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="logfile" />
    </rules>
</nlog>

但我无法在当前目录中创建 txt 文件。

最佳答案

试试这个...

<targets>
    <target name="logfile" xsi:type="File" fileName="${basedir}/file.txt" />
</targets>

也看看这里,introduction to NLog .

关于c# - 如何使用 NLog 在当前目录中创建文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198211/

相关文章:

C# 相当于 VB6 'Type'

c# - 无法在 C# 控制台应用程序中捕获 Ctrl+C

windows - 以编程方式在 R Console 中设置字体和字体大小?

azure - 使用 nlog.config 在 Azure Function Log Streams 中进行 NLog

c# - 在哪里写日志文件?

c# - NLog:IsTraceEnabled 的意外行为

c# - 将代码注入(inject)所有没有自定义属性的方法和属性的最简单方法

c# - Lambda 表达式 if-else 语句在 where 子句中

c# - 无法识别的配置部分 applicationSettings

java - 将 Java 控制台置于屏幕前面