示例#1
0
        /// <summary>
        /// Writes lines to the log file.
        /// </summary>
        /// <returns>The task.</returns>
        private static async Task WriteLogFileAsync()
        {
            // get the Logs folder
            Folder = ApplicationData.Current.LocalFolder;
            Folder = await Folder.CreateFolderAsync(FOLDER_NAME, CreationCollisionOption.OpenIfExists);

            // get the two log files
            File2 = await Folder.CreateFileAsync("2.log", CreationCollisionOption.OpenIfExists);

            File1 = await Folder.CreateFileAsync("1.log", CreationCollisionOption.OpenIfExists);

            // get the current log file
            BasicProperties prop1 = await File1.GetBasicPropertiesAsync();

            BasicProperties prop2 = await File2.GetBasicPropertiesAsync();

            CurrentFile = (prop1.DateModified >= prop2.DateModified) ? File1 : File2;
            var stream = await CurrentFile.OpenStreamForWriteAsync();

            stream.Seek(0, SeekOrigin.End);

            // process log file events
            List <string> myMessages = new List <string>();

            while (true)
            {
                // wait for an event
                signal.WaitOne();

                // clear the log files
                if (clear)
                {
                    CurrentFile = File1 = await Folder.CreateFileAsync("1.log", CreationCollisionOption.ReplaceExisting);

                    File2 = await Folder.CreateFileAsync("2.log", CreationCollisionOption.ReplaceExisting);

                    clear = false;
                    Cleared.Invoke(null, EventArgs.Empty);
                }

                // get the messages to be written
                lock (messages)
                {
                    myMessages.AddRange(messages);
                    messages.Clear();
                }

                // write the messages to the current log file
                foreach (string message in myMessages)
                {
                    Debug.WriteLine(message);
                    try
                    {
                        byte[] bytes = Encoding.UTF8.GetBytes(message);
                        await stream.WriteAsync(bytes);

                        stream.WriteByte(13);
                        await stream.FlushAsync();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("AppendTextAsync EXCEPTION: " + ex.Message);
                    }
                }
                myMessages.Clear();

                // switch log files if necessary
                BasicProperties prop = await CurrentFile.GetBasicPropertiesAsync();

                if (prop.Size >= maxFileSize)
                {
                    if (CurrentFile == File1)
                    {
                        CurrentFile = File2 = await Folder.CreateFileAsync("2.log", CreationCollisionOption.ReplaceExisting);
                    }
                    else
                    {
                        CurrentFile = File1 = await Folder.CreateFileAsync("1.log", CreationCollisionOption.ReplaceExisting);
                    }
                    stream = await CurrentFile.OpenStreamForWriteAsync();

                    stream.Seek(0, SeekOrigin.End);
                }
            }
        }