0

I try to use NLog in my Uwp application. I've added NLog nuget package to the project, created NLog.config file in the project directory, maken it Copy always

<?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"
      internalLogLevel="Trace"
      internalLogFile="C:\NlogLogs.txt"
      internalLogToConsole="true"
      internalLogToConsoleError="true"
      internalLogToTrace="true">

  <targets>
    <target concurrentWrites="false" name="logfile" xsi:type="File" fileName="file.txt" />
    <target name="logconsole" xsi:type="Console" />
  </targets>

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

Added logger into MainViewModel class (see below), and tried to log, but it hasn't worked. There aren't any file.txt in my bin/Debug folder or anywhere else, but no exceptions are occurred.

Why? How can I setup logging into UWP project?

 public class MainViewModel : ViewModelBase
    {

        private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public MainViewModel()
        {

            logger.Trace("Started MainViewModel");
            logger.Info("Started MainViewModel");
        }
Foo Boy
  • 9
  • 4
  • Try adding option `concurrentWrites="false"` to the logfile-target. See also https://github.com/NLog/NLog/wiki/File-target . Also good idea to check the NLog InternalLogger: https://github.com/NLog/NLog/wiki/Logging-troubleshooting – Rolf Kristensen Jun 25 '20 at 18:58
  • @RolfKristensen I've added concurrentWrites and Internal Logging into NLog.config file (you may see updates above in the question description), but I can see neither `file.txt` nor `NlogLogs.txt` . It is problem with finding config file? – Foo Boy Jul 02 '20 at 14:59
  • Could be an issue with including NLog.config on publish. ` PreserveNewest PreserveNewest ` in your csproj-file. – Rolf Kristensen Jul 02 '20 at 21:21
  • There is also the issue with permissions to file-path, where UWP apps can be limited. Maybe try one of these `${specialfolder}`-options: https://github.com/NLog/NLog/wiki/File-target#filename-directory – Rolf Kristensen Jul 02 '20 at 21:32
  • 1
    @RolfKristensen Thanks. It works for `${specialfolder:folder=CommonApplicationData}`, `${specialfolder:folder=ApplicationData}`, `${specialfolder:folder=LocalApplicationData}` . Can I somehow logs into another folders (e.g. bin folder of the current project)? – Foo Boy Jul 03 '20 at 12:41
  • You can try `${processdir}` with NLog 4.7.2 – Rolf Kristensen Jul 03 '20 at 16:01
  • @RolfKristensen I have NLog 4.7.2, but there aren't any log files in bin/Debug folder. It is description of my target ` ` – Foo Boy Jul 04 '20 at 15:16
  • Sounds like you should try and get the NLog InternalLogger working, so you can see what is going on. https://github.com/NLog/NLog/wiki/Logging-troubleshooting – Rolf Kristensen Jul 05 '20 at 11:08
  • @RolfKristensen I've tried your advice and the Internal log shows `Exception: System.UnauthorizedAccessException: Access to the path 'D:\Application\bin\x64\Debug\AppX\2020-07-05 16_52_07.2275.log' is denied.` when creating file. As I understand UWP apps allow to create files only in Local storage? – Foo Boy Jul 05 '20 at 14:00
  • Never developed UWP-apps, so know very little. Maybe this can help: https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions – Rolf Kristensen Jul 05 '20 at 20:48
  • @RolfKristensen Thanks, for the support. How should I close the question? – Foo Boy Jul 12 '20 at 09:48
  • If you have an answer to your own question, then just write it and choose it as answer. – Rolf Kristensen Jul 12 '20 at 15:59

0 Answers0