Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a text file in my current directory with NLog?

Tags:

c#

console

nlog

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.

like image 677
user2968369 Avatar asked Nov 25 '13 16:11

user2968369


People also ask

Where does NLog write to by default?

By default, the web. nlog file can be found at: C:\Program Files (x86)\Pleasant Solutions\Pleasant Password Server\www\web.

What can be configured with this NLog config file?

The following types can be configured: Targets - the destinations of a logevent, e.g. file, database, console. Layout - the layout e.g. json, csv, plain-text (default) Layout renderers - the template markers, e.g. ${message}, ${exception}, ${date}

What is a NLog file?

NLog is a logging framework for . NET. It has rich log routing and management capabilities and greatly helps you to produce and manage logs. NLog supports structured logs, multiple logging targets, and everything a modern logging framework should support.


2 Answers

Try this...

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

Take a look here too, introduction to NLog.

like image 54
Christian Phillips Avatar answered Sep 23 '22 00:09

Christian Phillips


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

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

like image 37
Nick V Avatar answered Sep 24 '22 00:09

Nick V