سؤال

I am using Nlog for the first time. My aim is to just write to a text file.

In main.c I have

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");
    }
}

My Nlog.config file is as follows:

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

But I am not able to create a txt file in my current directory.

هل كانت مفيدة؟

المحلول 2

Did you set the NLog.config 'copy to output directory' to 'copy always'?

You should get it to work if you follow their tutorial.

نصائح أخرى

Try this...

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

Take a look here too, introduction to NLog.

This worked for me ${CurrentDir}

e.g.

<target xsi:type="File" 
    name="ownFile-web" 
    fileName="${CurrentDir}\Logs\nlog-web-${shortdate}.log"
    layout="${longdate}|${event-properties:item=EventId_Id}| [${uppercase:${level}}]${newline}Logger: ${logger}${newline}url: ${aspnet-request-url}${newline}CallSite: ${callsite}${newline}Message: ${message}${onexception:${newline}EXCEPTION:${exception:format=tostring}}${newline}-------${newline}" />

https://github.com/NLog/NLog/wiki/CurrentDir-Layout-Renderer

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top