Embedding NLog in app.config for WPF application

Viewed 816

I added NLog to a WPF application I'm developing, using the NLog.config file. I added some log messages which were entered into a log file when I ran the application, all working fine.

I then decided to embed NLog in the app.config file instead, which according to the documentation is possible. However this wasn't working at all, no log file created, and internal logging not working on NLog either. To test I decided to build an empty WPF application and embed NLog in the app.config file with very a simple target and rule. It is behaving the same way, no logging happening at all and no internal logging from NLog either. When I look at my WpfApp.dll.config file, it is using the app.config file.

I'm not sure what I'm doing wrong here, why is it not working?

Edited to add: I have the latest NLog NuGet package (4.7.0), and do not have an NLog.config file in this application.

App.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <nlog internalLogFile="C:\intlog.txt" internalLogLevel="Trace" throwexceptions="true">
    <targets>
      <target name="logfile" type="file" fileName="mylogfile.txt" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
    </rules>
  </nlog>
</configuration>

MainWindow.xaml.cs

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            LogManager.ThrowExceptions = true;
            Logger mylogger = LogManager.GetLogger("foo");
            mylogger.Info("Program started");
            InitializeComponent();

        }
    }
}
0 Answers
Related