예제 #1
0
        public static HealthcheckResult RunHealthcheck(string fileNameFormat, string directoryPath, DateTime itemCreationDate, int numberDaysToCheck, bool isRemote = false)
        {
            var checkResult = new HealthcheckResult
            {
                LastCheckTime  = DateTime.UtcNow,
                Status         = Customization.HealthcheckStatus.Healthy,
                HealthyMessage = "There is no new error since the last check",
                ErrorList      = new ErrorList
                {
                    Entries = new List <ErrorEntry>()
                }
            };

            if (string.IsNullOrEmpty(fileNameFormat))
            {
                checkResult.Status = Customization.HealthcheckStatus.Warning;
                checkResult.ErrorList.Entries.Add(new ErrorEntry
                {
                    Created   = DateTime.UtcNow,
                    Reason    = "Log File Check is not configured correctly",
                    Exception = null
                });

                return(checkResult);
            }

            try
            {
                var directory = GetLogFileDirectory(directoryPath);
                var files     = directory.GetFiles(fileNameFormat);

                if (files == null || files.Count() == 0)
                {
                    checkResult.Status = HealthcheckStatus.Warning;
                    checkResult.ErrorList.Entries.Add(new ErrorEntry
                    {
                        Created   = DateTime.UtcNow,
                        Reason    = string.Format("No files can be found with the following pattern: {0}", fileNameFormat),
                        Exception = null
                    });

                    return(checkResult);
                }

                LogReaderSettings logReaderSettings = new LogReaderSettings(itemCreationDate, DateTime.MaxValue);

                if (numberDaysToCheck > 0)
                {
                    if (isRemote)
                    {
                        logReaderSettings.StartDateTime = itemCreationDate.ToLocalTime();
                    }
                    else
                    {
                        logReaderSettings.StartDateTime = DateTime.Now.AddDays(-numberDaysToCheck).Date;
                    }
                    logReaderSettings.FinishDateTime = DateTime.Now;
                }

                LogDataSource logDataSource = new LogDataSource(files, logReaderSettings);
                logDataSource.ParseFiles();

                var result = logDataSource.LogData;

                if (result.Errors != null && result.Errors.Count > 0)
                {
                    checkResult.Status = HealthcheckStatus.Error;
                    foreach (var error in result.Errors)
                    {
                        checkResult.ErrorList.Entries.Add(new ErrorEntry
                        {
                            Created   = error.Time,
                            Reason    = error.Message?.Message,
                            Exception = null
                        });
                    }
                }
                else if (result.Warns != null && result.Warns.Count > 0)
                {
                    checkResult.Status = HealthcheckStatus.Warning;
                    foreach (var warn in result.Warns)
                    {
                        checkResult.ErrorList.Entries.Add(new ErrorEntry
                        {
                            Created   = warn.Time,
                            Reason    = warn.Message?.Message,
                            Exception = null
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                checkResult.Status = HealthcheckStatus.Error;
                checkResult.ErrorList.Entries.Add(new ErrorEntry
                {
                    Created   = DateTime.UtcNow,
                    Reason    = exception.Message,
                    Exception = exception
                });
            }

            return(checkResult);
        }
        private async void ExportLogs_Click(object sender, RoutedEventArgs e)
        {
            if (IsAdmin())
            {
                var activeDB = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("SamplesDB.db");

                var buffer = await Windows.Storage.FileIO.ReadBufferAsync(activeDB);

                var savePicker = new Windows.Storage.Pickers.FileSavePicker
                {
                    SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder
                };
                // Dropdown of file types the user can save the file as
                savePicker.FileTypeChoices.Add("Plain Text", new List <string>()
                {
                    ".txt"
                });
                // Default file name if the user does not type one in or select a file to replace
                savePicker.SuggestedFileName = "LOGS";

                // Fetch list of all logs
                //List<string> logs = LogDataSource.GetLogs();
                var           LogList = LogDataSource.GetLogs();
                List <string> logs    = new List <string>();
                logs.Add("Emp_id, LotNum, WhenModified, Patient_id, Rep_id, LogType");
                foreach (Log l in LogList)
                {
                    logs.Add(l.empID + ", " + l.LotNum + ", " + l.LastModified + ", " + l.PatientID + ", " + l.RepID + ", " + l.LogType);
                }

                Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();

                if (file != null)
                {
                    // Prevent updates to the remote version of the file until
                    // we finish making changes and call CompleteUpdatesAsync.
                    Windows.Storage.CachedFileManager.DeferUpdates(file);
                    // write to file
                    await Windows.Storage.FileIO.WriteLinesAsync(file, logs);

                    // Let Windows know that we're finished changing the file so
                    // the other app can update the remote version of the file.
                    // Completing updates may require Windows to ask for user input.
                    Windows.Storage.Provider.FileUpdateStatus status =
                        await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);

                    if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
                    {
                        ExportSuccess.Text = "File " + file.Name + " was saved.";
                    }
                    else
                    {
                        ExportSuccess.Text = "File " + file.Name + " couldn't be saved.";
                    }
                }
                else
                {
                    ExportSuccess.Text = "Operation cancelled or interrupted.";
                }
                ExportSuccess.Visibility = Visibility.Visible;
            }
            else
            {
                PrivilegeError();
            }
        }