Answered by:
How to write a log or text file to the file system in Metro Style App?

Question
-
As the title said, I would like to write an activity log of my application into a file automatically. My application is built in Javascript/HTML5, when I am debugging I can use
console.log("this is a debug log")
and track it in console. But if I want to give my test machine to a certain test user, I want to be able to track the activity logs.Is there any way that I can do that? Is there API that I can use to write a file in the file system?
Louis
Friday, April 13, 2012 12:24 AM
Answers
-
The FileIO Class may work for you. There is a method for appending text to a file:
Windows.Storage.FileIO.appendTextAsync(file, contents).done( /* Your success and error handlers */ );
Your application will have to request permission to write to a file in the user's Documents folder via the manifest and the user will have to agree the first time your application tries to access that directory.
Check out this Quickstart for examples.
Because of the asynchronous nature of these operations, for logging purposes, you should consider sending your messages to a string or array of strings and then writing/appending them to your log file at one time--say when the app is about to suspend.
Friday, April 13, 2012 1:00 AM
All replies
-
The FileIO Class may work for you. There is a method for appending text to a file:
Windows.Storage.FileIO.appendTextAsync(file, contents).done( /* Your success and error handlers */ );
Your application will have to request permission to write to a file in the user's Documents folder via the manifest and the user will have to agree the first time your application tries to access that directory.
Check out this Quickstart for examples.
Because of the asynchronous nature of these operations, for logging purposes, you should consider sending your messages to a string or array of strings and then writing/appending them to your log file at one time--say when the app is about to suspend.
Friday, April 13, 2012 1:00 AM -
Hello,
I tried the above method but it doesn't work for me:
varLogFile = "LogFile.txt";
Windows.Storage.DownloadsFolder.createFileAsync("LogFile.txt").done(
function(file) {
LogFile = file;
});
Windows.Storage.FileIO.appendLinesAsync(LogFile, "Test").done(); <-- line 26
but then I get the following error:
Unhandled exception at line 26, column 5 in ms-appx://windowsmultimediaperformanceapplication/js/default.js
0x800a000d - JavaScript runtime error: Type mismatch
It creates the file properly but it fails to write in it. Any help will be appreciated. I looked at the sample code provided and referred to that when writing this.
Wednesday, June 5, 2013 5:50 PM -
I guess that it's a timing issue.
The Call to createFileAsync runs asynchronous. The done function which sets LogFile = file runs whenever createFileAsync finished.
That means that your code in Line 26
Windows.Storage.FileIO.appendLinesAsync(LogFile, "Test").done();
runs immediately after
Windows.Storage.DownloadsFolder.createFileAsync
but before
function(file) { LogFile = file; }
You can solve your problem by putting the appendLinesAsync call into the done function.
varLogFile = "LogFile.txt"; Windows.Storage.DownloadsFolder.createFileAsync("LogFile.txt").done( function(file) { LogFile = file; Windows.Storage.FileIO.appendLinesAsync(LogFile, "Test").done(); });
Thursday, June 6, 2013 10:51 AM