示例#1
0
        /// <summary>
        /// Create the message from the exception
        /// </summary>
        /// <returns></returns>
        private string CreateMessage(CustomException error)
        {
            // Create StringBuilder
            StringBuilder sb = new StringBuilder("Error: ");

            // append display text
            sb.Append(error.DisplayText);

            // append new line
            sb.Append(Environment.NewLine);

            // append method name
            sb.Append("Method: ");
            sb.Append(error.MethodName);

            // append new line
            sb.Append(Environment.NewLine);

            // apend object name
            sb.Append("Object: ");
            sb.Append(error.ObjectName);

            // append new line
            sb.Append(Environment.NewLine);

            // append user
            string user = "******";

            // If UserID
            if (error.UserID > 0)
            {
                // append user
                user = "******" + error.UserID.ToString();
            }

            // append user
            sb.Append(user);

            // append new line
            sb.Append(Environment.NewLine);

            // date
            sb.Append(DateTime.Now.ToString());

            // append new line
            sb.Append(Environment.NewLine);

            // return value
            return(sb.ToString());
        }
示例#2
0
        /// <summary>
        /// This method logs an error to a file.
        /// </summary>
        /// <param name="error"></param>
        private void LogErrorToFile(CustomException error)
        {
            // local
            StreamWriter writer = null;

            // Verify Log File Exists
            if (!String.IsNullOrEmpty(this.LogFileName))
            {
                // check if file exists
                if (File.Exists(this.LogFileName))
                {
                    // Create writer while appending text to the file
                    writer = File.AppendText(this.LogFileName);
                }
                else
                {
                    // Create Writer for create
                    writer = File.CreateText(this.LogFileName);
                }

                // Create Message
                string message = CreateMessage(error);

                // Write Message
                writer.WriteLine(message);

                // flush writer
                writer.Flush();

                // close stream
                writer.Close();

                // set writer to nothing
                writer = null;
            }
        }
示例#3
0
 /// <summary>
 /// This method logs errors to the system event log.
 /// </summary>
 /// <param name="exception"></param>
 private void LogErrorAsSystemEvent(CustomException exception)
 {
     throw new Exception("Log as system event log not implemented yet.");
 }
示例#4
0
        /// <summary>
        /// This method reads the app.config file to
        /// load the current configuration.
        /// </summary>
        /// <returns></returns>
        public void ConnectToDatabase(DataManager dataManager)
        {
            // locals
            string methodName = "ConnectToDatabase";
            string objectName = "AuthenticationManager";

            // Create variable for a possible CustomException
            CustomException exception = null;

            try
            {
                // If connection is not set
                if (String.IsNullOrEmpty(dataManager.DataConnector.ConnectionString))
                {
                    // if the connectionName is set
                    if (TextHelper.Exists(dataManager.ConnectionName))
                    {
                        // Set the ConnectionString (requires DataJuggler.Core.UltimateHelper version 1.3.5 or greater)
                        dataManager.DataConnector.ConnectionString = EnvironmentVariableHelper.GetEnvironmentVariableValue(dataManager.ConnectionName);
                    }
                    else
                    {
                        // this is for environments using a web.config or app.config.

                        // For Dot Net Core / Blazor or any new projects, Environment Variables are now the
                        // recommended way to store connection strings.

                        // Read Configuration File
                        this.Configuration.Read();

                        // If the configuration is valid
                        if (this.Configuration.IsValid)
                        {
                            // if the ConnectionString exists
                            if (this.Configuration.HasConnectionString)
                            {
                                // Set the connection string using Integrated Secrity (Windows Authentication)
                                dataManager.DataConnector.ConnectionString = this.Configuration.ConnectionString;
                            }
                            else
                            {
                                // set the server
                                string serverName = this.Configuration.DatabaseServer;

                                // set the databaseName
                                string databaseName = this.Configuration.DatabaseName;

                                // If integrated security is set to true
                                if (this.Configuration.IntegratedSecurity)
                                {
                                    // Set the connection string using Integrated Secrity (Windows Authentication)
                                    dataManager.DataConnector.ConnectionString = dataManager.DataConnector.BuildConnectionString(serverName, databaseName);
                                }
                                else
                                {
                                    // set the userName
                                    string userName = this.Configuration.DatabaseUserName;

                                    // set the password
                                    string password = this.Configuration.DatabasePassword;

                                    // build the connectionstring for Sql Server Authentication
                                    dataManager.DataConnector.ConnectionString = dataManager.DataConnector.BuildConnectionString(serverName, databaseName, userName, password);
                                }
                            }
                        }
                    }
                }

                // check if database is already connected
                if (dataManager.DataConnector.State == System.Data.ConnectionState.Open)
                {
                    // close connection and reopen (there should not be any open connections here)
                    // I have been thinking of a bulk insert feature in the future, but that is not for this method
                    // To Do: Log Error 'Database Connection Was Already Open'
                    dataManager.DataConnector.Close();
                }

                // Open Connection
                dataManager.DataConnector.Open();
            }
            catch (Exception error)
            {
                // If ErrorProcessor exists
                if (this.ErrorProcessor != null)
                {
                    // Only create a new configuration error if it does not already exist.
                    if (error as CustomException == null)
                    {
                        // If the configuration is not valid
                        if (!this.Configuration.IsValid)
                        {
                            // Create Instance of configurationError
                            exception = new InvalidConfigurationException(methodName, objectName, error);
                        }
                        else
                        {
                            // Create Instance of dataConnectionError
                            exception = new DataConnectionFailedException(methodName, objectName, error);
                        }
                    }

                    // Log this error
                    this.ErrorProcessor.LogError(methodName, objectName, exception);
                }
            }
        }