Beispiel #1
0
 /// <summary>
 /// Reloads the <see cref="LogFile"/> which entails re-downloading the file from the network path.
 /// </summary>
 public void ReloadAsync()
 {
     Task.Run(() =>
     {
         AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"User requested a reload of log file with network path \"{NetworkFile.FullName}\".");
         var keepCurrentAfterReload = KeepCurrent;
         Notification?.Clear();
         HasError    = false;
         KeepCurrent = false;
         Task.Delay(100).Wait();
         var showWaitMessage = true;
         var stopWatch       = new Stopwatch();
         stopWatch.Start();
         Initialized = false;
         InitializeAsync(NetworkFile.FullName, true);
         Task.Delay(500).Wait();
         while (!Initialized && !HasError)
         {
             if (showWaitMessage)
             {
                 AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Waiting for log entry with network path \"{NetworkFile.FullName}\" to re-initialize before setting the KeepCurrent property as part of Reload operation.", LogMessageType.Verbose);
                 showWaitMessage = false;
             }
             Task.Delay(100).Wait();
         }
         stopWatch.Stop();
         AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Re-initialization of log file with network path \"{NetworkFile.FullName}\" took {stopWatch.Elapsed.Seconds} s {stopWatch.Elapsed.Milliseconds} ms. Setting KeepCurrent property to {keepCurrentAfterReload}", LogMessageType.Verbose);
         KeepCurrent = keepCurrentAfterReload;
         ParentLogFolder.DecrementNumberOfLogFilesReloading();
         AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Reload of log file with network path \"{NetworkFile.FullName}\" complete.", LogMessageType.Verbose);
     });
 }
Beispiel #2
0
        /// <summary>
        /// Initializes the <see cref="LogFile"/>.
        /// </summary>
        /// <param name="path">The log file's path.</param>
        private void InitializeAsync(string path, bool hasAlreadyBeenInitialized = false)
        {
            Task.Run(() =>
            {
                Initialized = false;
                ParentLogFolder.IncrementNumberOfLogFilesInitializing();

                var showWaitMessage = true;
                while (Updating)
                {
                    if (showWaitMessage)
                    {
                        AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Waiting for log entry with network path \"{NetworkFile.FullName}\" to cancel update before initializing.", LogMessageType.Verbose);
                        showWaitMessage = false;
                    }
                    Task.Delay(100).Wait();
                }

                AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Initializing log file with network path \"{path}\".", LogMessageType.Verbose);
                NetworkFile   = new FileInfo(path);
                var localPath = AppInfo.LocalLogFileFolderPath + $"UniqueId{_uniqueId}_" + ParentLogFolder.ComputerName + "_" + NetworkFile.Name;
                var stopWatch = new Stopwatch();
                AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Downloading local copy of log file from network path \"{NetworkFile.FullName}\" to local path \"{localPath}\".", LogMessageType.Verbose);
                var copySucceded    = false;
                Exception exception = null;
                stopWatch.Start();
                while (!copySucceded && stopWatch.Elapsed.Minutes < 2)
                {
                    try
                    {
                        LocalFile    = NetworkFile.CopyTo(localPath, true);
                        copySucceded = true;
                    }
                    catch (Exception ex)
                    {
                        var waitTimeInMilliseconds = 3000;
                        exception = ex;
                        AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Download of local copy of log file with network path \"{NetworkFile.FullName}\" failed. Error message: {exception.Message} Trying again after {waitTimeInMilliseconds} milliseconds.", LogMessageType.Verbose);
                        Task.Delay(waitTimeInMilliseconds).Wait();
                    }
                }
                stopWatch.Stop();

                if (copySucceded)
                {
                    try
                    {
                        AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Download of local copy of log file with network path \"{NetworkFile.FullName}\" completed in {stopWatch.Elapsed.Minutes} m {stopWatch.Elapsed.Seconds} s {stopWatch.Elapsed.Milliseconds} ms. Size {LocalFile.Length / 1000} KB.");
                    }
                    catch (Exception ex)
                    {
                        AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Failed to log completion of log file download. Error message: {ex.Message}", LogMessageType.Warning);
                    }
                }
                else
                {
                    var errorMessageString = exception != null ? $"Error message: {exception.Message}" : string.Empty;
                    AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Download of local copy of log file with network path \"{NetworkFile.FullName}\" failed after {stopWatch.Elapsed.Minutes} m {stopWatch.Elapsed.Seconds} s {stopWatch.Elapsed.Milliseconds} ms. {errorMessageString}", LogMessageType.Error);

                    HasError     = true;
                    Notification = new Notification(this, $"Could not download local copy of log file with network path \"{NetworkFile.FullName}\".");
                }

                if (!hasAlreadyBeenInitialized)
                {
                    //Extract the creation date from the file name
                    var extractAppNameFromFileName = true;
                    try
                    {
                        var year     = NetworkFile.Name.Substring(2, 2);
                        var month    = NetworkFile.Name.Substring(4, 2);
                        var day      = NetworkFile.Name.Substring(6, 2);
                        CreationDate = $"{month}/{day}/{year}";
                        Convert.ToDateTime(CreationDate);
                    }
                    catch (Exception ex)
                    {
                        NetworkFile.Refresh();
                        if (NetworkFile.Exists)
                        {
                            AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Could not extract creation date from log file with path \"{NetworkFile.FullName}\" Showing windows file system creation time instead. Error message: {ex.Message}", LogMessageType.Verbose);
                            CreationDate = NetworkFile.CreationTime.ToString("MM/dd/yy");
                        }
                        else
                        {
                            AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Could not set creation date for log file with path \"{NetworkFile.FullName}\" becuase the file no longer exists.", LogMessageType.Warning);
                        }
                        extractAppNameFromFileName = false;
                    }

                    //Set parent log folder full path
                    Source1 = ParentLogFolder.Folder.FullName;

                    //Extract the application name from the file name
                    if (extractAppNameFromFileName)
                    {
                        try
                        {
                            var startingPoint = 9;
                            Source2           = NetworkFile.Name.Substring(startingPoint, NetworkFile.Name.Length - startingPoint - 4);
                        }
                        catch (Exception ex)
                        {
                            Source2 = "Unknown";
                            AppInfo.BaseAppInfo.Log.QueueLogMessageAsync($"Could not extract application name from log file with path \"{NetworkFile.FullName}\". Error message: {ex.Message}", LogMessageType.Error);
                        }
                    }
                    else
                    {
                        Source2 = NetworkFile.Name;
                    }
                }

                ParentLogFolder.DecrementNumberOfLogFilesInitializing();

                Initialized = !HasError;
            });
        }