Nlog: Log to file in UWP application

Created on 3 Aug 2017  路  8Comments  路  Source: NLog/NLog

Type Bug
NLog version: 5.0.0-beta09
Platform: .NET Core (UWP)
Current NLog config

<?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">
  <extensions>
    <add assembly="Logger"/>
  </extensions>
  <targets>
    <target name="file" xsi:type="File" fileName="${basedir}\nlog.txt" layout="${date}|${level}|${message}|${exception:format=tostring}" />
    <target name="TcpOutlet" xsi:type="NLogViewer" address="tcp4://127.0.0.1:4505" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="file" />
    <logger name="*" minlevel="Trace" writeTo="TcpOutlet" />
  </rules>
</nlog>
  • What is the current result?
    Exception thrown when trying to write log to file:
    NLog:2017-08-03 13:03:17.5194 Debug Creating file appender: D:\Logger\LoggerGUI\bin\x86\Release\AppX\nlog.txt NLog:2017-08-03 13:03:17.5194 Trace Opening D:\Logger\LoggerGUI\bin\x86\Release\AppX\nlog.txt with allowFileSharedWriting=False Exception thrown: 'System.UnauthorizedAccessException' in System.IO.FileSystem.dll NLog:2017-08-03 12:46:10.0910 Error Failed write to file 'D:\Logger\LoggerGUI\bin\x86\Release\AppX\nlog.txt'. Exception: System.UnauthorizedAccessException: UnauthorizedAccess_IODenied_Path, D:\Logger\LoggerGUI\bin\x86\Release\AppX\nlog.txt at LoggerGUI!<BaseAddress>+0x34266d at LoggerGUI!<BaseAddress>+0x345e0e at LoggerGUI!<BaseAddress>+0x3463f5 at LoggerGUI!<BaseAddress>+0x228e44 at NLog.Internal.FileAppenders.BaseFileAppender.UpdateCreationTime() at NLog.Internal.FileAppenders.BaseFileAppender.TryCreateFileStream(Boolean allowFileSharedWriting) at NLog.Internal.FileAppenders.BaseFileAppender.CreateFileStream(Boolean allowFileSharedWriting) at NLog.Internal.FileAppenders.RetryingMultiProcessFileAppender.Write(Byte[] bytes, Int32 offset, Int32 count) at NLog.Targets.FileTarget.WriteToFile(String fileName, LogEventInfo logEvent, ArraySegment<System.Byte> bytes, Boolean justData) NLog:2017-08-03 12:46:10.0910 Debug FileAppender Closing Invalidate - D:\Logger\LoggerGUI\bin\x86\Release\AppX\nlog.txt Exception thrown: 'System.UnauthorizedAccessException' in NLog.dll
  • What is the expected result?
    Write log to file in the base directory
  • Did you checked the [Internal log]?
    Yes the result is shown above.
  • Please post full exception details (message, stacktrace, inner exceptions)
    See above
  • Are there any work arrounds? no
  • Is there a version in which it did worked? no
file-target question

Most helpful comment

@shimonAzulay If you look at the UnauthorizedAccessException saying Failed write to file, then it is pretty obvious that you don't have write-access to the ${basedir}-directory.

I'm guessing that UWP-Store-Apps are very restricted to where they are allowed to write files. When looking at the Windows documentation:

var storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
NLog.GlobalDiagnosticsContext.Set("LogPath", storageFolder.Path + "\\");
<?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="file" xsi:type="File" fileName="${gdc:LogPath}nlog.txt" layout="${date}|${level}|${message}|${exception:format=tostring}" concurrentWrites="false" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>
</nlog>

Alternative you can try and access LOCALAPPDATA using Environment-layout-renderer.

All 8 comments

@shimonAzulay If you look at the UnauthorizedAccessException saying Failed write to file, then it is pretty obvious that you don't have write-access to the ${basedir}-directory.

I'm guessing that UWP-Store-Apps are very restricted to where they are allowed to write files. When looking at the Windows documentation:

var storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
NLog.GlobalDiagnosticsContext.Set("LogPath", storageFolder.Path + "\\");
<?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="file" xsi:type="File" fileName="${gdc:LogPath}nlog.txt" layout="${date}|${level}|${message}|${exception:format=tostring}" concurrentWrites="false" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>
</nlog>

Alternative you can try and access LOCALAPPDATA using Environment-layout-renderer.

@304NotModified @snakefoot Thank you guys for the help! It worked.

Guess it's thanks to @snakefoot ;)

Sorry but I'm not able to log even if I use the suggested method

@marinsky Please create new issue, and fill out the template: https://github.com/NLog/NLog/issues/new/choose

@snakefoot, I guess you meant to ask @msavazzi create a new issue.

Oops thx :) @msavazzi Please create new issue, and fill out the template: https://github.com/NLog/NLog/issues/new/choose

Oops thx :) @msavazzi Please create new issue, and fill out the template: https://github.com/NLog/NLog/issues/new/choose

Done :) all details is here #3893. Let me know if I can help more

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FaMouZx3 picture FaMouZx3  路  3Comments

npandrei picture npandrei  路  3Comments

MaximRouiller picture MaximRouiller  路  3Comments

smeegoan picture smeegoan  路  3Comments

sszost picture sszost  路  3Comments