private static EmailMessage getErrorEmailMessage(string body) { return(new EmailMessage { Subject = "Error in {0}".FormatWith(ConfigurationStatics.InstallationConfiguration.SystemName) + (ConfigurationStatics.IsClientSideProgram ? " on {0}".FormatWith(EwlStatics.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(EwlStatics.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> /// Reports an error to the developers. The report includes the specified exception and additional information about the running program. The prefix /// provides additional information before the standard exception and page information. /// </summary> public static void ReportError(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(); } sw.WriteLine("Application: {0}".FormatWith(ConfigurationStatics.AppName)); sw.WriteLine("Version: {0}".FormatWith(ConfigurationStatics.AppAssembly.GetName().Version)); if (!ConfigurationStatics.IsDevelopmentInstallation) { sw.WriteLine(); sw.WriteLine("Installation: {0}".FormatWith(ConfigurationStatics.InstallationConfiguration.InstallationName)); sw.WriteLine("Machine: {0}".FormatWith(Tewl.Tools.NetTools.GetLocalHostName())); } 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(); 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 = AppTools.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) : "")); } } } EwlStatics.CallEveryMethod( () => { lock ( errorEmailLimiter ) { errorEmailLimiter.RequestAction( () => EmailStatics.SendDeveloperNotificationEmail(getErrorEmailMessage(sw.ToString())), () => SendDeveloperNotification( "An error occurred and the email rate-limit was reached! See the log file for this and any other errors that may occur in the near future."), () => {}); } }, () => logError(sw.ToString())); } }
public bool Equals(InitializationAwareValue <T> other) { return(other != null && Initialized && other.Initialized && EwlStatics.AreEqual(value, other.value)); }
/// <summary> /// Initializes the system. 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="globalInitializer">The system's global initializer. Do not pass null.</param> /// <param name="appName"></param> /// <param name="isClientSideApp"></param> /// <param name="assemblyFolderPath">Pass a nonempty string to override the assembly folder path, which is used to locate the installation folder. Use with /// caution.</param> /// <param name="mainDataAccessStateGetter">A method that returns the current main data-access state whenever it is requested, including during this /// InitStatics 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> /// <param name="useLongDatabaseTimeouts">Pass true if the application is a background process that can tolerate slow database access.</param> public static void InitStatics( SystemInitializer globalInitializer, string appName, bool isClientSideApp, string assemblyFolderPath = "", Func <DataAccessState> mainDataAccessStateGetter = null, bool useLongDatabaseTimeouts = false) { var initializationLog = "Starting init"; try { if (initialized) { throw new ApplicationException("This class can only be initialized once."); } if (globalInitializer == null) { throw new ApplicationException("The system must have a global initializer."); } // Initialize these before the exception handling block below because it's reasonable for the exception handling to depend on them. ConfigurationStatics.Init(assemblyFolderPath, globalInitializer.GetType(), appName, isClientSideApp, ref initializationLog); EmailStatics.Init(); TelemetryStatics.Init(); // 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; EwlStatics.EmergencyLog("Initialization log", initializationLog); throw; } try { CultureInfo.DefaultThreadCurrentCulture = Cultures.EnglishUnitedStates; var asposePdfLicensePath = EwlStatics.CombinePaths(ConfigurationStatics.InstallationConfiguration.AsposeLicenseFolderPath, "Aspose.PDF.lic"); if (File.Exists(asposePdfLicensePath)) { new Aspose.Pdf.License().SetLicense(asposePdfLicensePath); } var asposeWordsLicensePath = EwlStatics.CombinePaths(ConfigurationStatics.InstallationConfiguration.AsposeLicenseFolderPath, "Aspose.Words.lic"); if (File.Exists(asposeWordsLicensePath)) { new Aspose.Words.License().SetLicense(asposeWordsLicensePath); } AppMemoryCache.Init(); BlobStorageStatics.Init(); DataAccessStatics.Init(); DataAccessState.Init(mainDataAccessStateGetter, useLongDatabaseTimeouts); EncryptionOps.Init(); HtmlBlockStatics.Init(); UserManagementStatics.Init(); GlobalInitializationOps.globalInitializer = globalInitializer; globalInitializer.InitStatics(); } 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 { TelemetryStatics.ReportError("An exception occurred during application initialization:", e); } catch {} } }
/// <summary> /// Initializes the system. 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="globalInitializer">The system's global initializer. Do not pass null.</param> /// <param name="appName"></param> /// <param name="isClientSideProgram"></param> /// <param name="useRelativeInstallationPath">Pass true to use a relative path for the installation folder. This means that the folder will be located using /// the working directory rather than the assembly path. Use with caution.</param> /// <param name="mainDataAccessStateGetter">A method that returns the current main data-access state whenever it is requested, including during this /// InitStatics 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 InitStatics( SystemInitializer globalInitializer, string appName, bool isClientSideProgram, bool useRelativeInstallationPath = false, Func <DataAccessState> mainDataAccessStateGetter = null) { var initializationLog = "Starting init"; try { if (initialized) { throw new ApplicationException("This class can only be initialized once."); } if (globalInitializer == null) { throw new ApplicationException("The system must have a global initializer."); } // 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(useRelativeInstallationPath, globalInitializer.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; EwlStatics.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); } AppMemoryCache.Init(); BlobFileOps.Init(); DataAccessStatics.Init(); DataAccessState.Init(mainDataAccessStateGetter); EmailStatics.Init(); EncryptionOps.Init(); HtmlBlockStatics.Init(); InstallationSupportUtility.ConfigurationLogic.Init1(); UserManagementStatics.Init(); GlobalInitializationOps.globalInitializer = globalInitializer; globalInitializer.InitStatics(); } 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 { TelemetryStatics.ReportError("An exception occurred during application initialization:", e); } catch {} } }
public bool Equals(DataValue <T> other) { return(other != null && EwlStatics.AreEqual(val, other.val)); }
/// <summary> /// Reports an error to the developers. The report includes the specified exception and additional information about the running program. The prefix /// provides additional information before the standard exception and page information. /// </summary> public static void ReportError(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 = AppTools.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: " + EwlStatics.GetLocalHostName()); } EwlStatics.CallEveryMethod( delegate { EmailStatics.SendDeveloperNotificationEmail(getErrorEmailMessage(sw.ToString())); }, delegate { logError(sw.ToString()); }); } }