예제 #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="message">Message of the Log</param>
        /// <remarks>The message is staored as part of the exception parent class</remarks>
        public Log(string message, string user = "", string exmsg = "") : base(message)
        {
            this.LogDate      = System.DateTime.Now;
            this.LogUserName  = user;
            this.LogMessageEX = exmsg;

            //get configured store type if set use it otherwise use the default
            string StoreTypeConfiguration = ConfigurationManager.AppSettings.Get("StoreType");

            if (StoreTypeConfiguration != null)
            {
                if (StoreTypeConfiguration.Length > 0)
                {
                    this.StoreType = StoreTypeConfiguration;
                }
            }

            //get the confiured dll namespace or use the default DLL of LogLoader
            string StoreNamespace = ConfigurationManager.AppSettings.Get("StoreNamespace");

            if (StoreNamespace != null)
            {
                if (StoreNamespace.Length > 0)
                {
                    this.StoreNameSpace = StoreNamespace;
                }
            }

            //create the storage method based on config entries using reflection
            //call the factory pattern so that we can create the required log implementation
            _StoreStragtegy = LogClassFactory.CreateLogImplementation(this.StoreNameSpace, this.StoreType);
        }
예제 #2
0
        /// <summary>
        /// Creates the required log class based on the IStrorageStrategy interface
        /// </summary>
        /// <returns>a class of type IStrorageStrategy</returns>
        /// <remarks>factory pattern method the default returned value in the event of an error is a filestore</remarks>
        public static IStrorageStrategy CreateLogImplementation(string StoreNamespace, string StoreType)
        {
            //only reflect if required improved perfmoance for performance
            if (_StoreStragtegy == null)
            {
                // generate dynamic object based on configured value using reflection
                Type StorageTypeClass = null;

                // get a handle to the current .net assmbley i could load any assmebly for isolation reasons all storage code
                // could be in a single dll rather than in the main dll
                Module[] RunningAssembly = Assembly.GetExecutingAssembly().GetModules(false);

                //I know there is only one assmbley for this app some extra code will be required if not
                Module myModule = RunningAssembly[0];

                //If the config is asking to load up a non default DLL we need
                // to get it in to the running assmbley App Domain should use the loaded dll name rather than a string
                if (StoreNamespace != "LogLoader")
                {
                    Assembly LoadedAssembly = Assembly.Load(StoreNamespace); // this will try to find the class

                    //bind to the require class in the loaded DLL file based on config values
                    StorageTypeClass = LoadedAssembly.GetType(StoreNamespace + ".DataStores." + StoreType, false, false);
                }
                else
                {
                    //default only need is to lod the required class in the bas LogLoader running DLL
                    //get the reflected type class based on the provided configured string
                    StorageTypeClass = myModule.GetType(StoreNamespace + "." + StoreType, false, false);
                }

                //null is thrown back if type not found so check for it other wise fallback to default
                if (StorageTypeClass != null)
                {
                    //the configured storage
                    _StoreStragtegy = Activator.CreateInstance(StorageTypeClass) as IStrorageStrategy;
                }
                else
                {
                    //the default setup based on LoagLoader not a custom DLL file
                    Type DefaultStorageTypeClass = myModule.GetType("LogLoader.FileStore", false, false);
                    _StoreStragtegy = Activator.CreateInstance(DefaultStorageTypeClass) as IStrorageStrategy;
                }
            }

            return(_StoreStragtegy);
        }