8

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?

Miroslav Popov
  • 3,294
  • 4
  • 32
  • 55
  • 1
    Maybe this article will help you: [http://coding.infoconex.com/post/2012/06/01/Getting-LINQPad-to-read-your-applications-AppConfig-settings](http://coding.infoconex.com/post/2012/06/01/Getting-LINQPad-to-read-your-applications-AppConfig-settings). – keenthinker Jun 03 '14 at 20:37
  • 1
    Your code and config is working fine, if you copy the `nlog.config` next to your LINQPad.exe and when LINQPad.exe also has a write access to its folder. So if you running LINQPad.exe you need to run as admin or configure Nlog to put the logfile somewhere else. – nemesv Jun 03 '14 at 20:46
  • Hmm. It was necessary to restart LINQPad in order to read the config file. However this is not a solution so far. LINQPad has its own `LINQPad.exe.config`. I added NLog tags in it but it doesn't work. If I make a separate `LINQPad.config` with NLog tags only, LINQPad warns that cannot find its `` tags. – Miroslav Popov Jun 03 '14 at 20:46
  • @nemesv, your solution works! The problem was that I haven't restarted LINQPad. – Miroslav Popov Jun 03 '14 at 20:50

3 Answers3

12

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");
  • Very helpful; I was able to implement this technique in IPython to load a configuration file from the working directory. – Mike C Jul 02 '15 at 22:05
4

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)

Miroslav Popov
  • 3,294
  • 4
  • 32
  • 55
2

As a minor alternative to the solution offered in Valeriu Caraulean's answer you could add a method like the following to configure NLog but use an existing configuration file:

public static void ConfigureNLog()
{
    LogManager.Configuration = new XmlLoggingConfiguration(@"C:\path\to\existing\config\file\NLog.xml");
    LogManager.ReconfigExistingLoggers();
}

You'll want to add the following to the namespace imports for your LINQPad query:

NLog
NLog.Config
Community
  • 1
  • 1
Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93