public static bool GetBool(string p_Name, bool p_DefaultValue) { string methSig = $"GetBool({p_Name},{p_DefaultValue})"; RegistryKey currentUser = Registry.CurrentUser; TraceLogger.Debug($"{methSig}: RegistryKey[CurrentUser]='{currentUser?.Name??"NULL"}'"); currentUser.CreateSubKey("Software\\ASCOM\\Utilities"); RegistryKey registryKey = currentUser.OpenSubKey("Software\\ASCOM\\Utilities", true); TraceLogger.Debug($"{methSig}: RegistryKey[Utilities]='{registryKey?.Name ?? "NULL"}'"); bool flag = false; try { if (registryKey.GetValueKind(p_Name) == RegistryValueKind.String) { string keyValue = registryKey.GetValue(p_Name).ToString(); TraceLogger.Debug($"{methSig}: RegistryKey[Utilities.{p_Name}]='{keyValue ?? "NULL"}'"); flag = Conversions.ToBoolean(keyValue); } } catch (IOException ex1) { TraceLogger.Debug($"Exception1: Get RegistryValue: {ex1.GetType().Name}"); TraceLogger.Debug(ex1.ToString()); //ProjectData.SetProjectError((Exception) ex1); try { RegistryCommonCode.SetName(p_Name, p_DefaultValue.ToString()); flag = p_DefaultValue; } catch (Exception ex2) { TraceLogger.Debug($"Exception2: SetName: {ex2.GetType().Name}"); TraceLogger.Debug(ex2.ToString()); //ProjectData.SetProjectError(ex2); flag = p_DefaultValue; //ProjectData.ClearProjectError(); } //ProjectData.ClearProjectError(); } catch (Exception ex) { TraceLogger.Debug($"Exception: Get RegistryValue: {ex.GetType().Name}"); TraceLogger.Debug(ex.ToString()); //ProjectData.SetProjectError(ex); flag = p_DefaultValue; //ProjectData.ClearProjectError(); } registryKey.Flush(); registryKey.Close(); currentUser.Flush(); currentUser.Close(); return(flag); }
public static void LogEvent(string Caller, string Msg, EventLogEntryType Severity, GlobalConstants.EventLogErrors Id, string Except) { try { TraceLogger.Debug($"EventLog[{Severity}][{Id}]:[{Caller}]: {Msg}"); if (!String.IsNullOrEmpty(Except)) { TraceLogger.Debug($"Exception: {Except}"); } if (!EventLog.SourceExists("ASCOM Platform")) { EventLog.CreateEventSource("ASCOM Platform", "ASCOM"); EventLog eventLog1 = new EventLog("ASCOM", ".", "ASCOM Platform"); eventLog1.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0); eventLog1.MaximumKilobytes = 1024L; eventLog1.Close(); eventLog1.Dispose(); Thread.Sleep(3000); EventLog eventLog2 = new EventLog("ASCOM", ".", "ASCOM Platform"); eventLog2.WriteEntry("Successfully created event log - Policy: " + eventLog2.OverflowAction.ToString() + ", Size: " + Conversions.ToString(eventLog2.MaximumKilobytes) + "kb", EventLogEntryType.Information, 0); eventLog2.Close(); eventLog2.Dispose(); } EventLog eventLog = new EventLog("ASCOM", ".", "ASCOM Platform"); string message = Caller + " - " + Msg; if (Except != null) { message = message + "\r\n" + Except; } eventLog.WriteEntry(message, Severity, (int)Id); eventLog.Close(); eventLog.Dispose(); } catch (Win32Exception ex1) { //ProjectData.SetProjectError((Exception) ex1); Win32Exception win32Exception = ex1; try { string str = Strings.Format((object)DateTime.Now, "dd MMMM yyyy HH:mm:ss.fff"); string path1 = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ASCOM\\Logs\\EventLog.Errors.txt"; string path2 = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ASCOM\\Logs\\EventLog.Messages.txt"; File.AppendAllText(path1, str + " ErrorCode: 0x" + Conversion.Hex(win32Exception.ErrorCode) + " NativeErrorCode: 0x" + Conversion.Hex(win32Exception.NativeErrorCode) + " " + win32Exception.ToString() + "\r\n"); File.AppendAllText(path2, str + " " + Caller + " " + Msg + " " + Severity.ToString() + " " + Id.ToString() + " " + Except + "\r\n"); } catch (Exception ex2) { //ProjectData.SetProjectError(ex2); //ProjectData.ClearProjectError(); } //ProjectData.ClearProjectError(); } catch (Exception ex1) { //ProjectData.SetProjectError(ex1); Exception exception = ex1; try { string str = Strings.Format((object)DateTime.Now, "dd MMMM yyyy HH:mm:ss.fff"); string path1 = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ASCOM\\Logs\\EventLogErrors.txt"; string path2 = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ASCOM\\Logs\\EventLogMessages.txt"; File.AppendAllText(path1, str + " " + exception.ToString() + "\r\n"); File.AppendAllText(path2, str + " " + Caller + " " + Msg + " " + Severity.ToString() + " " + Id.ToString() + " " + Except + "\r\n"); } catch (Exception ex2) { //ProjectData.SetProjectError(ex2); //ProjectData.ClearProjectError(); } //ProjectData.ClearProjectError(); } }