コード例 #1
0
        public int GetThreadId()
        {
            int threadId = 0;

            if (UnmanagedCodePermissionAvailable)
            {
                try
                {
                    threadId = ChoKernel32Core.GetCurrentThreadId();
                }
                catch (Exception e)
                {
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ChoApplicationException.ToString(e));
                    }
                }
            }
            else
            {
                if (ChoTrace.ChoSwitch.TraceError)
                {
                    Trace.WriteLine("Failed to retrieve value due to unmanaged code permission denied.");
                }
            }

            return(threadId);
        }
コード例 #2
0
ファイル: ChoMessageBox.cs プロジェクト: lanicon/Cinchoo
        public ChoMessageBox(Exception ex, string errMsg = null, string title = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(ex, "Exception");

            InitializeComponent();
            Initialize(ChoApplicationException.ToString(ex), errMsg, title);
        }
コード例 #3
0
ファイル: ChoPlugInEditor.cs プロジェクト: lanicon/Cinchoo
        private string FormatException(ChoPlugIn plugIn, Exception ex, string errMsg = null)
        {
            StringBuilder msg = new StringBuilder();

            if (plugIn != null)
            {
                msg.AppendLine("Error found running '{0}' plugin.".FormatString(plugIn.Name));
            }
            else
            {
                msg.AppendLine(errMsg.IsNullOrWhiteSpace() ? "Error found running all plugins." : errMsg);
            }

            msg.AppendLine("Error Msg: {0}.".FormatString(ex.Message));
            msg.AppendLine();
            msg.AppendLine("StackTrace:");
            msg.AppendLine(ChoApplicationException.ToString(ex));

            return(msg.ToString());
        }
コード例 #4
0
        /// <summary>
        /// Unhandled exception handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //TODO: able to hook into user dll method
            string    errorMsg = "An application error occurred. Please contact the adminstrator with the following information:\n\n";
            Exception ex       = (Exception)e.ExceptionObject;

            if (ex != null)
            {
                errorMsg = ChoApplicationException.ToString(ex);
                ChoApplication.WriteToEventLog(errorMsg);
                //errorMsg += ex.Message + "\n\nStack Trace:\n" + ex.StackTrace;
                //ChoApplication.WriteToEventLog(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace, EventLogEntryType.Error);
            }
            else
            {
                ChoApplication.WriteToEventLog(errorMsg + "Unknown exception occured.", EventLogEntryType.Error);
            }

            Console.WriteLine(errorMsg);
            Environment.FailFast(errorMsg, ex);
        }
コード例 #5
0
        public string GetThreadName()
        {
            string threadName = null;

            try
            {
                threadName = Thread.CurrentThread.Name;
            }
            catch (Exception e)
            {
                if (ChoTrace.ChoSwitch.TraceError)
                {
                    Trace.WriteLine(ChoApplicationException.ToString(e));
                }
            }
            if (String.IsNullOrEmpty(threadName))
            {
                threadName = String.Format("ChoThread {0}", GetThreadId());
            }

            return(threadName);
        }
コード例 #6
0
        //[ChoSingletonInstanceInitializer]
        public void Initialize()
        {
            if (_initialized)
            {
                return;
            }

            lock (_padLock)
            {
                if (_initialized)
                {
                    return;
                }

                _initialized = true;

                try
                {
                    EntryAssemblyLocation = ChoAssembly.GetEntryAssembly().Location;
                    EntryAssemblyFileName = System.IO.Path.GetFileName(EntryAssemblyLocation);
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                try
                {
                    AppEnvironment = ConfigurationManager.AppSettings["appEnvironment"];
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.ToString());
                }

                try
                {
                    SharedEnvironmentConfigFilePath = ConfigurationManager.AppSettings["sharedEnvironmentConfigFilePath"];
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.ToString());
                }

                try
                {
                    if (ConfigurationManager.AppSettings["appConfigPath"].IsNullOrWhiteSpace())
                    {
                        ApplicationConfigFilePath = System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                    }
                    else
                    {
                        ApplicationConfigFilePath = ConfigurationManager.AppSettings["appConfigPath"].Trim();
                    }
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                try
                {
                    ApplicationBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                #region Check for Unmanaged Code Permission Available

                // check whether the unmanaged code permission is available to avoid three potential stack walks
                SecurityPermission unmanagedCodePermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
                // avoid a stack walk by checking for the permission on the current assembly. this is safe because there are no
                // stack walk modifiers before the call.
                if (SecurityManager.IsGranted(unmanagedCodePermission))
                {
                    try
                    {
                        unmanagedCodePermission.Demand();
                        UnmanagedCodePermissionAvailable = true;
                    }
                    catch (SecurityException e)
                    {
                        if (ChoTrace.ChoSwitch.TraceError)
                        {
                            Trace.WriteLine(ChoApplicationException.ToString(e));
                        }
                    }
                }

                #endregion Check for Unmanaged Code Permission Available

                EventLogSourceName = ChoApplicationSettings.State.EventLogSourceName;

                #region GetApplicationName

                try
                {
                    ApplicationName = ChoApplicationSettings.State.ApplicationId;
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }

                    try
                    {
                        ApplicationName = System.IO.Path.GetFileName(EntryAssemblyLocation);
                    }
                    catch (System.Security.SecurityException e)
                    {
                        ChoTrace.Error(ChoApplicationException.ToString(e));
                    }
                }

                ApplicationNameWithoutExtension = Path.GetFileNameWithoutExtension(ApplicationName);
                if (!ChoApplicationSettings.State.LogFolder.IsNullOrWhiteSpace())
                {
                    ApplicationLogDirectory = ChoApplicationSettings.State.LogFolder;
                }
                else if (ChoApplicationSettings.State.UseApplicationDataFolderAsLogFolder)
                {
                    ApplicationLogDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ApplicationNameWithoutExtension, ChoReservedDirectoryName.Logs);
                }
                else
                {
                    ApplicationLogDirectory = Path.Combine(ApplicationBaseDirectory, ChoReservedDirectoryName.Logs);
                }

                #endregion GetApplicationName

                #region Get AppDomainName

                try
                {
                    AppDomainName = AppDomain.CurrentDomain.FriendlyName;
                }
                catch (Exception ex)
                {
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                #endregion Get AppDomainName

                #region Get ProcessId, ProcessName

                if (UnmanagedCodePermissionAvailable)
                {
                    try
                    {
                        ProcessId = ChoKernel32Core.GetCurrentProcessId();
                    }
                    catch (Exception ex)
                    {
                        if (ChoTrace.ChoSwitch.TraceError)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                    }

                    try
                    {
                        ProcessFilePath = GetProcessName();
                    }
                    catch (Exception ex)
                    {
                        if (ChoTrace.ChoSwitch.TraceError)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                    }
                }
                else
                {
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine("Failed to retrieve value due to unmanaged code permission denied.");
                    }
                }

                #endregion Get ProcessId, ProcessName

                #region Get HostName

                // Get the DNS host name of the current machine
                try
                {
                    // Lookup the host name
                    HostName = System.Net.Dns.GetHostName();
                }
                catch (System.Net.Sockets.SocketException)
                {
                }
                catch (System.Security.SecurityException)
                {
                    // We may get a security exception looking up the hostname
                    // You must have Unrestricted DnsPermission to access resource
                }

                // Get the NETBIOS machine name of the current machine
                if (HostName.IsNullOrWhiteSpace())
                {
                    try
                    {
                        HostName = Environment.MachineName;
                    }
                    catch (InvalidOperationException)
                    {
                    }
                    catch (System.Security.SecurityException)
                    {
                        // We may get a security exception looking up the machine name
                        // You must have Unrestricted EnvironmentPermission to access resource
                    }
                }

                #endregion Get HostName

                ApplyFrxParamsOverrides.Raise(this, null);
            }
        }
コード例 #7
0
ファイル: ChoEnvironment.cs プロジェクト: lanicon/Cinchoo
 public static void Exit(int exitCode, Exception ex)
 {
     Exit(exitCode, ex == null ? "Fatal application error occured." : ChoApplicationException.ToString(ex));
 }