/// <summary> /// Initializes the class. This includes loading application settings from the configuration file. The application name should be scoped within the system. /// For non web applications, this method must be called directly from the main executable assembly and not from a supporting library. /// /// To debug this method, create a folder called C:\AnyoneFullControl and give Everyone full control. A file will appear in that folder explaining how far /// it got in init. /// </summary> /// <param name="appName"></param> /// <param name="isClientSideProgram"></param> /// <param name="systemLogic"></param> /// <param name="mainDataAccessStateGetter">A method that returns the current main data-access state whenever it is requested, including during this /// AppTools.Init call. Do not allow multiple threads to use the same state at the same time. If you pass null, the data-access subsystem will not be /// available in the application.</param> public static void Init(string appName, bool isClientSideProgram, SystemLogic systemLogic, Func <DataAccessState> mainDataAccessStateGetter = null) { var initializationLog = "Starting init"; try { if (initialized) { throw new ApplicationException("This class can only be initialized once."); } if (systemLogic == null) { throw new ApplicationException("The system must have a global logic class and you must pass an instance of it to AppTools.Init."); } // Initialize ConfigurationStatics, including the general provider, before the exception handling block below because it's reasonable for the exception // handling to depend on this. ConfigurationStatics.Init(systemLogic.GetType(), appName, isClientSideProgram, ref initializationLog); // Setting the initialized flag to true must be done before executing the secondary init block below so that exception handling works. initialized = true; initializationLog += Environment.NewLine + "Succeeded in primary init."; } catch (Exception e) { initializationLog += Environment.NewLine + e; StandardLibraryMethods.EmergencyLog("Initialization log", initializationLog); throw; } try { var asposeLicense = ConfigurationStatics.SystemGeneralProvider.AsposeLicenseName; if (asposeLicense.Any()) { new Aspose.Pdf.License().SetLicense(asposeLicense); new Aspose.Words.License().SetLicense(asposeLicense); } // This initialization could be performed using reflection. There is no need for AppTools to have a dependency on these classes. AppMemoryCache.Init(); BlobFileOps.Init(); DataAccessStatics.Init(); DataAccessState.Init(mainDataAccessStateGetter); EncryptionOps.Init(); HtmlBlockStatics.Init(); InstallationSupportUtility.ConfigurationLogic.Init1(); UserManagementStatics.Init(); systemLogic.InitSystem(); } catch (Exception e) { secondaryInitFailed = true; // Suppress all exceptions since they would prevent apps from knowing that primary initialization succeeded. EWF apps need to know this in order to // automatically restart themselves. Other apps could find this knowledge useful as well. try { EmailAndLogError("An exception occurred during application initialization:", e); } catch {} } }
private static EmailMessage getErrorEmailMessage(string body) { return(new EmailMessage { Subject = "Error in {0}".FormatWith(ConfigurationStatics.InstallationConfiguration.SystemName) + (ConfigurationStatics.IsClientSideProgram ? " on {0}".FormatWith(StandardLibraryMethods.GetLocalHostName()) : ""), BodyHtml = body.GetTextAsEncodedHtml() }); }
/// <summary> /// This will tell people what to look for in the tests. /// Outputs a ReadMe file, with each iteration being a line in a item1: item2 format. /// </summary> internal static void OutputReadme(string outputFolder, IEnumerable <Tuple <string, string> > explanations) { using (var readme = new StreamWriter(StandardLibraryMethods.CombinePaths(outputFolder, "ReadMe.txt"))) { readme.WriteLine("What to look for"); readme.WriteLine(); foreach (var explanation in explanations) { readme.WriteLine("{0}: {1}".FormatWith(explanation.Item1, explanation.Item2)); readme.WriteLine(); } } }
/// <summary> /// Sends an error email message to the developer addresses specified in the config file using the SMTP server specified in the config file. /// </summary> private static void sendErrorEmail(string body) { assertClassInitialized(); var m = new EmailMessage(); foreach (var developer in ConfigurationStatics.InstallationConfiguration.Developers) { m.ToAddresses.Add(new EmailAddress(developer.EmailAddress, developer.Name)); } m.Subject = "Error in " + ConfigurationStatics.InstallationConfiguration.SystemName; if (ConfigurationStatics.IsClientSideProgram) { m.Subject += " on " + StandardLibraryMethods.GetLocalHostName(); } m.BodyHtml = body.GetTextAsEncodedHtml(); SendEmailWithDefaultFromAddress(m); }
public bool Equals(InitializationAwareValue <T> other) { return(other != null && Initialized && other.Initialized && StandardLibraryMethods.AreEqual(value, other.value)); }
/// <summary> /// Sends an email from the default address to the developers with the given exception information and additional information about /// the running program. Prefix provides additional information before the standard exception and page information. /// The exception may be null. /// </summary> public static void EmailAndLogError(string prefix, Exception exception) { using (var sw = new StringWriter()) { if (prefix.Length > 0) { sw.WriteLine(prefix); sw.WriteLine(); } if (exception != null) { sw.WriteLine(exception.ToString()); sw.WriteLine(); } if (NetTools.IsWebApp()) { // This check ensures that there is an actual request, which is not the case during application initialization. if (EwfApp.Instance != null && EwfApp.Instance.RequestState != null) { sw.WriteLine("URL: " + AppRequestState.Instance.Url); sw.WriteLine(); foreach (string fieldName in HttpContext.Current.Request.Form) { sw.WriteLine("Form field " + fieldName + ": " + HttpContext.Current.Request.Form[fieldName]); } sw.WriteLine(); foreach (string cookieName in HttpContext.Current.Request.Cookies) { sw.WriteLine("Cookie " + cookieName + ": " + HttpContext.Current.Request.Cookies[cookieName].Value); } sw.WriteLine(); sw.WriteLine("User agent: " + HttpContext.Current.Request.GetUserAgent()); sw.WriteLine("Referrer: " + NetTools.ReferringUrl); User user = null; User impersonator = null; // exception-prone code try { user = User; impersonator = AppRequestState.Instance.ImpersonatorExists ? AppRequestState.Instance.ImpersonatorUser : null; } catch {} if (user != null) { sw.WriteLine("User: {0}{1}".FormatWith(user.Email, impersonator != null ? " (impersonated by {0})".FormatWith(impersonator.Email) : "")); } } } else { sw.WriteLine("Program: " + ConfigurationStatics.AppName); sw.WriteLine("Version: " + ConfigurationStatics.AppAssembly.GetName().Version); sw.WriteLine("Machine: " + StandardLibraryMethods.GetLocalHostName()); } StandardLibraryMethods.CallEveryMethod(delegate { sendErrorEmail(sw.ToString()); }, delegate { logError(sw.ToString()); }); } }
public bool Equals(DataValue <T> other) { return(other != null && StandardLibraryMethods.AreEqual(val, other.val)); }