Exemple #1
0
 public void Fatal(string message, Exception exception)
 {
     if (_logger != null)
     {
         _logger.FatalException(message, exception);
     }
 }
Exemple #2
0
 public void Fatal(object message, Exception exception)
 {
     if (IsFatalEnabled)
     {
         log.FatalException(message.ToString(), exception);
     }
 }
Exemple #3
0
 public void Fatal(string message, Exception exception)
 {
     if (!ExcludeFomLog(message))
     {
         logger.FatalException(message, exception);
     }
 }
Exemple #4
0
 public void Fatal(string message, Exception exc = null)
 {
     if (exc == null)
     {
         InternalLogger.Fatal(message);
     }
     else
     {
         InternalLogger.FatalException(message, exc);
     }
 }
Exemple #5
0
 public void Run(CancellationTokenSource tokenSource)
 {
     _logger.Trace("[" + ID + "] Run() called.");
     RunCalled = true;
     try
     {
         RunInternal(tokenSource);
     }
     catch (Exception ex)
     {
         _logger.FatalException("Extension crashed!", ex);
         throw;
     }
 }
Exemple #6
0
        static void Main(string[] args)
        {
            ConfigurationItemFactory.Default.Targets.RegisterDefinition("ServiceManager", typeof(ServiceManagerTarget));

            string subdir = null, runDebugMethodOnExtension = null;
            var    baseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extensions");

            Environment.CurrentDirectory = ConfigurationManager.AppSettings["DataDirectory"] ?? AppDomain.CurrentDomain.BaseDirectory;
            var     extensionIDs = new HashSet <string>();
            Process process      = null;
            Guid    guid         = Guid.Empty;
            Logger  logger       = null;

            var options = new OptionSet
            {
                { "guid=", "Specifies a GUID that the extension can use to identify itself to the parent process", v =>
                  {
                      Guid id;
                      if (!Guid.TryParse(v, out id))
                      {
                          throw new OptionException("The specified id was not a valid GUID", "guid");
                      }
                      guid = id;
                  } },
                { "basedir=", "Specifies the base plugins directory (can be relative or absolute)", v => baseDir = Path.IsPathRooted(v) ? v : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, v) },
                { "subdir=", "Specifies the extension subdirectory name", v => subdir = v },
                { "debug=", "Specifies an extension ID to run the debug method on", v => runDebugMethodOnExtension = v },
                { "pid=", "Parent process ID - if specified, this process will close when the parent process closes", v =>
                  {
                      int pid;
                      if (!int.TryParse(v, out pid))
                      {
                          throw new OptionException("The parent process ID must be a 32 bit integer", "pid");
                      }
                      try
                      {
                          process = Process.GetProcessById(pid);
                      }
                      catch (Exception ex)
                      {
                          throw new OptionException(ex.Message, "pid");
                      }
                      if (process == null)
                      {
                          throw new OptionException("There is no process with ID [" + pid + "]", "pid");
                      }
                  } },
                { "<>", v => extensionIDs.Add(v) }
            };

            CancellationTokenSource src = new CancellationTokenSource();

            try
            {
                options.Parse(args);
                if (subdir == null)
                {
                    Console.Write("Enter plugin directory name (not the full path): ");
                    subdir = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(subdir))
                    {
                        Console.WriteLine("No plugin directory specified.");
                        Exit(null, src, ExtensionRunnerExitCode.InvalidArguments);
                    }
                }

                GlobalDiagnosticsContext.Set("ExeBaseDir", new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName);
                GlobalDiagnosticsContext.Set("SubDirName", subdir);
                GlobalDiagnosticsContext.Set("ParentProcess", process == null ? "" : process.Id.ToString());
                logger = LogManager.GetCurrentClassLogger();
                logger.Info(new [] {
                    "ExtensionRunner Started:",
                    "	=> Command Line: "+ Environment.CommandLine,
                    "	=> Subdirectory: "+ subdir,
                    "	=> Base Directory: "+ baseDir,
                    "	=> Specified Extensions: "+ extensionIDs.Concat(", "),
                    "	=> GUID: "+ guid,
                    "	=> Parent Process ID: "+ (process == null ? "(none)" : process.Id.ToString())
                }.Concat(Environment.NewLine));

                AppDomain.CurrentDomain.UnhandledException += (s, e) => logger.FatalException("UNTRAPPED SERVICE EXCEPTION", (Exception)e.ExceptionObject);
                TaskScheduler.UnobservedTaskException      += (s, e) => logger.FatalException("UNTRAPPED TASK EXCEPTION:", e.Exception);

                if (process != null)
                {
                    Task.Factory.StartNew(() =>
                    {
                        while (!src.IsCancellationRequested)
                        {
                            process.Refresh();
                            if (process.HasExited)
                            {
                                logger.Warn("Detected parent process shutdown.");
                                Exit(logger, src, ExtensionRunnerExitCode.ParentExited);
                                return;
                            }
                            Thread.Sleep(250);
                        }
                    });
                }

                // Read list of available extensions
                Dictionary <string, ExtensionInfo> extInfos;
                using (var loader = new SafeExtensionLoader(baseDir, subdir, process == null ? "" : process.Id.ToString(), src))
                    extInfos = loader.AvailableExtensions.ToDictionary(x => x.ExtensionID, x => x.Clone());

                if (extensionIDs.Count == 0)
                {
                    extensionIDs = new HashSet <string>(extInfos.Select(x => x.Key));                    // use all available extensions
                }
                else
                {
                    extensionIDs = new HashSet <string>(extensionIDs.Where(x => extInfos.ContainsKey(x)));                    // eliminate invalid any extension IDs
                }
                logger.Info("Active extensions: " + (extensionIDs.Any() ? extensionIDs.Concat(", ") : "(none)"));
                logger.Info("Inactive extensions: " + (!extensionIDs.Any() ? extInfos.Where(x => !extensionIDs.Contains(x.Key)).Concat(", ") : "(none)"));

                var extLoaders = new List <SafeExtensionLoader>();
                var extTasks   = new List <Task>();
                try
                {
                    foreach (var id in extensionIDs)
                    {
                        logger.Debug("Starting appdomain for extension: {0}", id);
                        var loader = new SafeExtensionLoader(baseDir, subdir, process == null ? "" : process.Id.ToString(), src);
                        var extID  = id;
                        extTasks.Add(Task.Factory.StartNew(() => loader.RunExtension(guid, runDebugMethodOnExtension == extID, extID)));
                    }
                    Task.WaitAll(extTasks.ToArray(), src.Token);
                }
                finally
                {
                    foreach (var extLoader in extLoaders)
                    {
                        extLoader.Dispose();
                    }
                }
                //using(var loader = new SafeExtensionLoader(baseDir, subdir, process == null ? "" : process.Id.ToString(), src))
                //{
                //    var runExtsTask = Task.Factory.StartNew(() =>
                //    {
                //        // Verify that all extensions are available and if so, run them
                //        var sb = new StringBuilder();
                //        sb.AppendLine("[list of all plugins]");
                //        foreach(var extInfo in loader.AllExtensions)
                //            sb.AppendLine("\t" + extInfo.ExtensionID + ": " + extInfo.Name + " [" + (extensionIDs.Count == 0 || extensionIDs.Contains(extInfo.ExtensionID) ? "ACTIVE" : "INACTIVE") + "]");
                //        logger.Info(sb.ToString());
                //        loader.RunExtensions(guid, runDebugMethodOnExtension, extensionIDs.ToArray());
                //    }, src.Token);

                //    loader.RunMainAppThread();
                //    Task.WaitAll(new[] { runExtsTask }, src.Token);
                //}
            }
            catch (OptionException ex)
            {
                if (logger != null)
                {
                    logger.Error("Invalid command options: " + ex.Message, options.WriteOptionDescriptions());
                }
                Exit(logger, src, ExtensionRunnerExitCode.Exception);
            }
            catch (Exception ex)
            {
                if (logger != null)
                {
                    logger.FatalException("An exception was thrown", ex);
                }
                Exit(logger, src, ExtensionRunnerExitCode.Exception);
            }
            finally
            {
                Exit(logger, src, ExtensionRunnerExitCode.Success);
            }
        }
 /// <summary>
 /// 输出错误日志 Fatal 级别。
 /// </summary>
 /// <param name="msg">错误消息体</param>
 /// <param name="ex">错误类</param>
 public void Fatal(object msg, Exception ex)
 {
     log4.FatalException(EncryptPassword(msg), ex);
 }
Exemple #8
0
 public void Fatal(string message, Exception ex)
 {
     logger.FatalException(AppendInfomation(message), ex);
 }
Exemple #9
0
 public static void Fatal(string message, Exception exception)
 {
     LogHelper.FatalException(message, exception);
 }
Exemple #10
0
        public static void AnyUnhandledExceptionHandler(object sender, Exception e, NLog.Logger nlogger)
        {
            if (e is AbandonedMutexException)
            {
                MessageBox.Show("Mutex exception");
                Application.Exit();
            }

            // Get IP address
            StringBuilder ipAddrs       = new StringBuilder();
            Regex         rxCheckIpAddr = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");

            foreach (NetworkInterface iface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (iface.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
                    iface.OperationalStatus != OperationalStatus.Up)
                {
                    continue;
                }
                foreach (IPAddressInformation ipInfo in iface.GetIPProperties().UnicastAddresses)
                {
                    string ipAddr = ipInfo.Address.ToString();
                    if (rxCheckIpAddr.IsMatch(ipAddr))
                    {
                        ipAddrs.Append(ipAddr).Append(", ");
                    }
                }
            }

            // Write to log
            nlogger.FatalException(
                string.Format("Unhandled exception.  IP:{0}", ipAddrs.ToString()), e);

            // Show dialog
            for (Exception innerEx = e.InnerException; innerEx != null; innerEx = innerEx.InnerException)
            {
                nlogger.FatalException("Inner exception", innerEx);
            }

            List <string> messageLines = new List <string>();

            messageLines.Add("Unexpected exception.  Please ask expert with information below.");
            messageLines.Add(" - Tester number");
            messageLines.Add(string.Format(" - Jade version {0}", Application.ProductVersion));
            messageLines.Add(string.Format(" - IP address: {0}", ipAddrs.ToString()));
            messageLines.Add(DateTime.Now.ToString(@" - Ti\me: yyyy/MM/dd HH:mm:ss"));
            messageLines.Add(string.Format(@" - Log file in {0}\logs", System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));
            messageLines.Add("");
            messageLines.Add(e.Message);
            messageLines.Add(e.StackTrace);
            if (e is TargetInvocationException && e.InnerException != null)
            {
                messageLines.Add("");
                messageLines.Add("Inner exception:");
                messageLines.Add(e.InnerException.Message);
            }
            string message = string.Join(Environment.NewLine, messageLines.ToArray());

            /*
             * LogWriter.Enable = true;
             * LogWriter.WriteToLogFile(message);
             * LogWriter.WriteToLogFile(e.StackTrace);
             */
            nlogger.FatalException("Unhandled exception", e);

            //DialogResult result = MessageBox.Show(
            //     message + e.StackTrace, "Unhandled Error.",
            //     MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);

            MessageBoxWith mb     = new MessageBoxWith(message + e.StackTrace, "Unhandled Error.");
            DialogResult   result = mb.ShowDialog();

            switch (result)
            {
            case DialogResult.Abort:
                nlogger.Fatal("Abort");
                Application.Exit();
                break;

            case DialogResult.Retry:
                nlogger.Fatal("Retry");
                Application.Exit();
                break;

            case DialogResult.Ignore:
                nlogger.Fatal("Ignore");
                Application.Exit();
                break;
            }
        }