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.
By default, the web. nlog file can be found at: C:\Program Files (x86)\Pleasant Solutions\Pleasant Password Server\www\web.
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}
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.
Try this...
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}/file.txt" />
</targets>
Take a look here too, introduction to NLog
.
Did you set the NLog.config 'copy to output directory' to 'copy always'?
You should get it to work if you follow their tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With