{"id":6559,"date":"2018-11-19T22:06:08","date_gmt":"2018-11-19T22:06:08","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6559"},"modified":"2018-11-19T22:08:38","modified_gmt":"2018-11-19T22:08:38","slug":"uwp-applications-file-restrictions-and-logging","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/uwp-applications-file-restrictions-and-logging\/","title":{"rendered":"UWP Application&#8217;s file restrictions and logging"},"content":{"rendered":"<p>UWP applications have restricted access to the local file system and this can cause a few issues when using logging frameworks if you&#8217;re expecting to write logs to c:\\Temp<br \/>\nor %Temp% locations (for example).<\/p>\n<p>Your UWP application can write to the application&#8217;s installation folder, for example <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar installedLocation = \r\n   Windows.ApplicationModel.Package.Current.InstalledLocation.Path;\r\n<\/pre>\n<p>another alternatively is application data location, such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar localFolder = \r\n   ApplicationData.Current.LocalFolder.Path;\r\n\r\n\/\/ or\r\n\r\nvar localCache = \r\n   ApplicationData.Current.LocalCacheFolder.Path;\r\n<\/pre>\n<p>These last two location will translate to a location such as the following, where username is the (as you&#8217;d expect) username the user logged into the machine with, and the GUID is the package family name GUID (taken from the application&#8217;s manifest). This is actually an extended GUID. <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nC:\\Users\\&lt;username&gt;\\AppData\\Local\\Packages\\&lt;guid&gt;\r\n<\/pre>\n<p><em>Note: At the time of writing I&#8217;m not sure where the string after the underscore which follows the GUID comes from.<\/em><\/p>\n<p>Let&#8217;s take a quick look at using Serilog&#8217;s File Sink to write our log files. Using NuGet install Serilog and Serilog.Sink.File and use the following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nLog.Logger = _logger = new LoggerConfiguration()\r\n   .WriteTo.File(\r\n      new JsonFormatter(renderMessage: true),\r\n      ApplicationData.Current.LocalCacheFolder.Path + &quot;\\\\log.txt&quot;, \r\n      rollingInterval: RollingInterval.Minute)\r\n      .MinimumLevel.Verbose()\r\n      .CreateLogger();\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/uwp\/files\/file-access-permissions\" rel=\"noopener\" target=\"_blank\">File access permissions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UWP applications have restricted access to the local file system and this can cause a few issues when using logging frameworks if you&#8217;re expecting to write logs to c:\\Temp or %Temp% locations (for example). Your UWP application can write to the application&#8217;s installation folder, for example var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation.Path; another alternatively is application data [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[125,106],"tags":[],"class_list":["post-6559","post","type-post","status-publish","format-standard","hentry","category-serilog","category-universal-app"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6559"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6559\/revisions"}],"predecessor-version":[{"id":6563,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6559\/revisions\/6563"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}