Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog via LINQPad - Where to put config file?

I'm trying to test NLog under LINQPad.

I successfully linked it and my code compiles well. However, NLog doesn't write log files because it is not configured.

I tried to make various config files like: NLog.config and LINQPad.config but it looks like I do not do it correctly.

My testing code under LINQPad is:

void Main()
{
    try
    {
        int zero = 0;
        int result = 5 / zero;
    }
    catch (DivideByZeroException ex)
    {
        Logger logger = LogManager.GetCurrentClassLogger();
        logger.ErrorException("Whoops!", ex);
    }
}

Config code:

<?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="logfile.log" />
    </targets>

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

Where to put the config file?

like image 761
Miroslav Popov Avatar asked Jun 03 '14 20:06

Miroslav Popov


People also ask

Where do I put NLog config?

Configuration File Just make a config called NLog. config in the root of your project. If you have a separate config file, make sure you set the "Copy to Output Directory" is set to "Copy Always" to avoid many tears wondering why the logging doesn't work.

How do I edit NLog config?

You can find the NLog. config file in {Project Folder}\bin\Debug\net5. 0\ . You can open and edit it as you like with Notepad or Visual Studio.

What is NLog config in C#?

NLog is a . Net Library that enables you to add high-quality logs for your application. NLog can also be downloaded using Nugget in Visual Studio. Targets are used to display, store, or pass log messages to another destination.


2 Answers

You can use LinqPad's extensions to enable logging: everything you'll drop to My Extensions script will be available for every other script.

Add a class to My Extensions with content:

public static class NLogLinqPadExtensions
{
    public static void ConfigureLogging()
    {
                var nlogConfig = @"
<nlog>
    <targets>
    <target name='console' type='Console' layout='${date:format=dd/MM/yy HH\:mm\:ss\:fff} | ${level:uppercase=true} | ${message} | ${exception:format=tostring}' />
    </targets>
    <rules>
    <logger name='*' minlevel='Debug' writeTo='console' />
    </rules>
</nlog>
        ";

        using (var sr = new StringReader(nlogConfig))
        {
            using (var xr = XmlReader.Create(sr))
            {
                NLog.LogManager.Configuration = new XmlLoggingConfiguration(xr, null);
                NLog.LogManager.ReconfigExistingLoggers();
            }
        }
    }
}

And don't forget to add NLog package to this script.

This example configures the console logger - you'll see your log messages in LinqPad's Results window, which is pretty cool for long-running processes and utility scripts.

To enable logging you have to call this extension from your script. Create a new LinqPad script and try this:

NLogLinqPadExtensions.ConfigureLogging();

var logger = LogManager.GetLogger("Foo");
logger.Debug("Hi");
like image 105
Valeriu Caraulean Avatar answered Sep 19 '22 07:09

Valeriu Caraulean


Thanks to @pasty and @nemesv comments, the problem is fixed.

The config file must be named NLog.config and must be placed in the LINQPad.exe folder.

The log file (logfile.log in my case) appears in the same folder.

LINQPad needs write access to the folder in order to write the log file.

LINQPad must be restarted after setting the config file.

[Edit]

If you start LINQPad, load or write the code and run it, the log file appears in the LINQPad.exe folder.

If you run the code by clicking on saved code file like NLog Test.linq in my case, the log file appears in the folder of the code file. (C:\Users\Miroslav\Documents\LINQPad Queries in my case)

like image 25
Miroslav Popov Avatar answered Sep 21 '22 07:09

Miroslav Popov