Beispiel #1
0
        /// <summary>
        /// Copies operation log from AppData to Desktop.
        /// </summary>
        public static async Task CopyOperationAsync()
        {
            var buffer = new StringBuilder();

            try
            {
                foreach (var fileName in await GetOperationFileNamesAsync())
                {
                    buffer.Append(await AppDataService.ReadAsync(fileName));
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Failed to read log from AppData." + Environment.NewLine
                                + ex);
            }

            if (buffer.Length == 0)
            {
                return;
            }

            if (MessageBox.Show(
                    Resources.CopyOperationMessage,
                    ProductInfo.Title,
                    MessageBoxButton.OKCancel,
                    MessageBoxImage.Information,
                    MessageBoxResult.OK) != MessageBoxResult.OK)
            {
                return;
            }

            RecordToDesktop(OperationFileName, buffer.ToString());
        }
Beispiel #2
0
        private static async Task <string[]> GetOperationFileNamesAsync()
        {
            var expiryDate      = DateTimeOffset.Now.Date.AddDays(-ExpiryDays);
            var fileNamePattern = new Regex(@"^operation(?<date>[0-9]{8}).log$");
            var fileNames       = new List <string>();

            foreach (var fileName in AppDataService.EnumerateFileNames("*.log").OrderBy(x => x))
            {
                var match = fileNamePattern.Match(fileName);
                if (match.Success)
                {
                    if (DateTimeOffset.TryParseExact(match.Groups["date"].Value,
                                                     DateFormat,
                                                     null,
                                                     DateTimeStyles.None,
                                                     out DateTimeOffset fileDate) &&
                        (expiryDate <= fileDate))
                    {
                        fileNames.Add(fileName);
                        continue;
                    }

                    AppDataService.Delete(fileName);
                }
            }

            #region Conversion

            var content = await AppDataService.ReadAsync(OperationFileName);

            if (content is not null)
            {
                if (fileNames.Any())
                {
                    content += await AppDataService.ReadAsync(fileNames.First());

                    await AppDataService.WriteAsync(fileNames.First(), append : false, content);

                    AppDataService.Delete(OperationFileName);
                }
                else
                {
                    var fileName = GetOperationDateFileName(DateTimeOffset.Now);
                    AppDataService.Rename(OperationFileName, fileName);
                    fileNames.Add(fileName);
                }
            }

            #endregion

            return(fileNames.ToArray());
        }
Beispiel #3
0
        private static void RecordToAppData(string fileName, string content, int capacity = 1)
        {
            try
            {
                AppDataService.AssureFolder();

                var appDataFilePath = Path.Combine(AppDataService.FolderPath, fileName);

                UpdateContent(appDataFilePath, content, capacity);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Failed to record log to AppData." + Environment.NewLine
                                + ex);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Records operation log to AppData.
        /// </summary>
        /// <param name="content">Content</param>
        public static async Task RecordOperationAsync(string content)
        {
            content = ComposeHeader() + Environment.NewLine
                      + content + Environment.NewLine + Environment.NewLine;

            var fileName = GetOperationDateFileName(DateTimeOffset.Now);

            try
            {
                await AppDataService.WriteAsync(fileName, append : true, content);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Failed to record log to AppData." + Environment.NewLine
                                + ex);
            }
        }