static eXtensibleWebApiConfig()
        {
            try
            {
                string configfilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                string configFolder   = Path.GetDirectoryName(configfilepath);

                WebApiPlugins = configFolder + "\\" + "webApiControllers";


                LoggingModeOption     loggingMode;
                LoggingStrategyOption loggingStrategy;
                string logToCandidate            = ConfigurationManager.AppSettings[XFAPIConstants.Config.LogToKey];
                string loggingModeCandidate      = ConfigurationManager.AppSettings[XFAPIConstants.Config.LoggingModeKey];
                string sqlConnectionKeyCandidate = ConfigurationManager.AppSettings[XFAPIConstants.Config.SqlConnectionKey];

                var configfilemap = new ExeConfigurationFileMap()
                {
                    ExeConfigFilename = configfilepath
                };
                Configuration           config  = ConfigurationManager.OpenMappedExeConfiguration(configfilemap, ConfigurationUserLevel.None);
                eXtensibleWebApiSection section = config.Sections[XFWebApiConstants.Config.SectionName] as eXtensibleWebApiSection;


                if (section != null)
                {
                    MessageProviderFolder = section.MessageProviderFolder;
                    var found = section.Elements.GetForLoggingMode(section.LoggingKey);

                    if (Enum.TryParse <LoggingStrategyOption>(found.LoggingStrategy, true, out loggingStrategy) &&
                        Enum.TryParse <LoggingModeOption>(found.LoggingMode, true, out loggingMode))
                    {
                        if (loggingStrategy == LoggingStrategyOption.Datastore)
                        {
                            SqlConnectionKey = found.DatastoreKey;
                        }
                        LoggingMode = loggingMode;
                        LogTo       = loggingStrategy;
                    }
                    else
                    {
                        loggingMode = XFAPIConstants.Default.LoggingMode;
                        LogTo       = XFAPIConstants.Default.LogTo;
                    }

                    IsEditRegistration = section.EditRegistration;
                    CatchAll           = section.CatchAllControllerId;
                    MessageIdHeaderKey = section.MessageIdHeaderKey;
                }
                else
                {
                    IsEditRegistration = false;
                    if (!String.IsNullOrWhiteSpace(loggingModeCandidate) && Enum.TryParse <LoggingModeOption>(loggingModeCandidate, true, out loggingMode))
                    {
                        LoggingMode = loggingMode;
                    }
                    else
                    {
                        LoggingMode = XFAPIConstants.Default.LoggingMode;
                    }
                    // read from appsettings or use defaults
                    if (!String.IsNullOrWhiteSpace(logToCandidate) && Enum.TryParse <LoggingStrategyOption>(logToCandidate, true, out loggingStrategy))
                    {
                        LogTo = loggingStrategy;
                    }
                    else
                    {
                        LogTo = XFAPIConstants.Default.LogTo;
                    }

                    if (!String.IsNullOrWhiteSpace(sqlConnectionKeyCandidate))
                    {
                        SqlConnectionKey = sqlConnectionKeyCandidate;
                    }
                    else
                    {
                        SqlConnectionKey = XFAPIConstants.Default.DatastoreConnectionKey;
                    }

                    if (LogTo.Equals(LoggingStrategyOption.Datastore))
                    {
                        bool b = false;
                        try
                        {
                            string cnText = ConfigurationManager.ConnectionStrings[SqlConnectionKey].ConnectionString;
                            using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnText))
                            {
                                cn.Open();
                                if (cn.State == System.Data.ConnectionState.Open)
                                {
                                    IsLogToDatastore = true;
                                }
                                else
                                {
                                    LogTo = XFAPIConstants.Default.LogTo;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogTo = LoggingStrategyOption.None;
                        }
                    }
                    MessageProviderFolder = XFAPIConstants.Default.MessageProviderFolder;
                    IsEditRegistration    = XFAPIConstants.Default.IsEditRegistration;
                    CatchAll           = XFAPIConstants.Default.CatchAllControllerId;
                    MessageIdHeaderKey = XFAPIConstants.Default.MessageIdHeaderKey;
                }
            }
            catch (Exception ex)
            {
                // nows setup defaults
                MessageProviderFolder = XFAPIConstants.Default.MessageProviderFolder;
                LoggingMode           = XFAPIConstants.Default.LoggingMode;
                LogTo = XFAPIConstants.Default.LogTo;
                IsEditRegistration = XFAPIConstants.Default.IsEditRegistration;
                CatchAll           = XFAPIConstants.Default.CatchAllControllerId;
                MessageIdHeaderKey = XFAPIConstants.Default.MessageIdHeaderKey;
            }
        }
Exemple #2
0
        static eXtensibleConfig()
        {
            try
            {
                string configfilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                string configFolder   = Path.GetDirectoryName(configfilepath);
                ModelPlugins               = configFolder + "\\" + "model";
                ModelDataGatewayPlugins    = configFolder + "\\" + "mdg";
                ServicePlugins             = configFolder + "\\" + "services";
                RemoteProcedureCallPlugins = configFolder + "\\" + "rpc";
                DbConfigs     = configFolder + "\\" + "db.configs";
                BaseDirectory = configFolder;
                var configfilemap = new ExeConfigurationFileMap()
                {
                    ExeConfigFilename = configfilepath
                };
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configfilemap, ConfigurationUserLevel.None);
                eXtensibleFrameworkSection section = config.Sections[XFConstants.Config.SectionName] as eXtensibleFrameworkSection;
                if (section != null)
                {
                    string candidateContext = ConfigurationManager.AppSettings[XFConstants.Application.Config.ApplicationKey];
                    string candidateZone    = ConfigurationManager.AppSettings[XFConstants.Application.Config.ZoneKey];
                    //string candidateLogging = ConfigurationManager.AppSettings[XFConstants.Application.Config.LoggingStrategyKey];
                    //string candidateLoggingSeverity = ConfigurationManager.AppSettings[XFConstants.Application.Config.LoggingSeverityKey];
                    string candidateConnectionStringKey = ConfigurationManager.AppSettings[XFConstants.Application.Config.ConnectionStringKey];
                    string candidateLogSource           = ConfigurationManager.AppSettings[XFConstants.Application.Config.LogSourceKey];
                    string candidateBigDataUrl          = ConfigurationManager.AppSettings[XFConstants.Application.Config.BigDataUrlKey];
                    string candidateServiceToken        = ConfigurationManager.AppSettings[XFConstants.Application.Config.ServiceTokenKey];
                    string candidateInstance            = ConfigurationManager.AppSettings[XFConstants.Application.Config.InstanceIdentifierKey];
                    string candidateInfer = ConfigurationManager.AppSettings[XFConstants.Application.Config.InferKey];

                    ZoneOption option;
                    if (Enum.TryParse <ZoneOption>(section.Zone, true, out option))
                    {
                        Zone = option.ToString();
                    }
                    else if (!String.IsNullOrEmpty(candidateZone) && Enum.TryParse <ZoneOption>(candidateZone, true, out option))
                    {
                        Zone = option.ToString();
                    }
                    else
                    {
                        Zone = ZoneOption.Development.ToString();
                    }

                    if (!String.IsNullOrEmpty(section.Context))
                    {
                        Context = section.Context;
                    }
                    else
                    {
                        Context = !String.IsNullOrEmpty(candidateContext) ? candidateContext : XFConstants.Application.DefaultAppplicationKey;
                    }

                    if (!String.IsNullOrEmpty(section.InstanceIdentifier))
                    {
                        if (section.InstanceIdentifier.Equals("machine", StringComparison.OrdinalIgnoreCase))
                        {
                            InstanceIdentifier = Environment.MachineName.ToLower();
                        }
                        else
                        {
                            InstanceIdentifier = section.InstanceIdentifier;
                        }
                    }
                    else if (!String.IsNullOrEmpty(candidateInfer))
                    {
                        if (candidateInfer.Equals("machine", StringComparison.OrdinalIgnoreCase))
                        {
                            InstanceIdentifier = Environment.MachineName.ToLower();
                        }
                        else
                        {
                            InstanceIdentifier = candidateInfer;
                        }

                        InstanceIdentifier = !String.IsNullOrEmpty(candidateInstance) ? candidateInstance : new Guid().ToString();
                    }
                    else
                    {
                        InstanceIdentifier = Environment.MachineName.ToLower();
                    }
                    bool infer;
                    if (!String.IsNullOrWhiteSpace(candidateInfer) && Boolean.TryParse(candidateInfer, out infer))
                    {
                        Infer = infer;
                    }
                    else
                    {
                        Infer = false;
                    }

                    bool inform = false;
                    var  found  = section.Elements.GetForLoggingMode(section.LoggingKey);
                    LoggingStrategyOption loggingStrategy;
                    if (Enum.TryParse <LoggingStrategyOption>(found.LoggingStrategy, true, out loggingStrategy))
                    {
                        LoggingStrategy = loggingStrategy;
                        inform          = found.Inform;
                    }
                    else
                    {
                        LoggingStrategy = LoggingStrategyOption.Silent;
                    }
                    Inform = inform;

                    TraceEventTypeOption eventType;
                    if (Enum.TryParse <TraceEventTypeOption>(found.PublishSeverity, true, out eventType))
                    {
                        LoggingSeverity = eventType;
                    }
                    else
                    {
                        LoggingSeverity = TraceEventTypeOption.Verbose;
                    }

                    if (!String.IsNullOrEmpty(section.LogSource))
                    {
                        LogSource = section.LogSource;
                    }
                    else if (!String.IsNullOrEmpty(candidateLogSource))
                    {
                        LogSource = candidateLogSource;
                    }
                    else
                    {
                        LogSource = XFConstants.Config.DefaultLogSource;
                    }


                    if (!String.IsNullOrEmpty(section.BigDataUrl))
                    {
                        BigDataUrl = section.BigDataUrl;
                    }
                    else if (!String.IsNullOrEmpty(candidateBigDataUrl))
                    {
                        BigDataUrl = candidateBigDataUrl;
                    }
                    else
                    {
                        //
                    }

                    if (!String.IsNullOrEmpty(section.ServiceToken))
                    {
                        ServiceToken = section.ServiceToken;
                    }
                    else if (!String.IsNullOrEmpty(candidateServiceToken))
                    {
                        ServiceToken = candidateServiceToken;
                    }
                    else
                    {
                        ServiceToken = XFConstants.Config.DefaultServiceToken;
                    }

                    if (!String.IsNullOrEmpty(candidateConnectionStringKey))
                    {
                        ConnectionStringKey = candidateConnectionStringKey;
                    }
                    CaptureMetrics = section.CaptureMetrics;

                    IsAsync = false;

                    AppUserIdentity        = XFConstants.Config.AppUserIdentityParamName;
                    DefaultLoggingCategory = XFConstants.Category.General;

                    ApiRoot    = XFConstants.Api.DefaultRoot;
                    ApiErrors  = XFConstants.Api.DefaultErrorsEndpoint;
                    ApiEvents  = XFConstants.Api.DefaultEventsEndpoint;
                    ApiStatii  = XFConstants.Api.DefaultStatiiEndpoint;
                    ApiMetrics = XFConstants.Api.DefaultMetricsEndpoint;
                    ApiAlerts  = XFConstants.Api.DefaultAlertsEndpoint;
                    ApiTasks   = XFConstants.Api.DefaultTasksEndpoint;
                    ApiKpi     = XFConstants.Api.DefaultKpiEndpoint;
                    ApiCustom  = XFConstants.Api.DefaultCustomEndpoint;
                }
                else
                {
                    section                = new eXtensibleFrameworkSection();
                    Zone                   = section.Zone = XFConstants.ZONE.Development;
                    Context                = section.Context = XFConstants.Config.DefaultApplicationKey;
                    IsAsync                = section.IsAsync = false;
                    InstanceIdentifier     = section.InstanceIdentifier = String.Empty;
                    AppUserIdentity        = section.UserIdentityParamName = XFConstants.Config.AppUserIdentityParamName;
                    DefaultLoggingCategory = XFConstants.Category.General;
                    LoggingStrategy        = LoggingStrategyOption.WindowsEventLog;
                    LoggingSeverity        = TraceEventTypeOption.Verbose;
                    LogSource              = XFConstants.Config.DefaultLogSource;
                    ServiceToken           = XFConstants.Config.DefaultServiceToken;
                    Infer                  = false;
                }
            }
            catch (Exception ex)
            {
                Exception              = ex.Message;
                Zone                   = XFConstants.Config.DefaultZone;
                DataPlugins            = XFConstants.Config.DATAACCESSCONTEXTFOLDERPATH;
                DefaultLoggingCategory = XFConstants.Category.General;
                LoggingStrategyOption option;
                if (Enum.TryParse <LoggingStrategyOption>(XFConstants.Config.DefaultLoggingStrategy, true, out option))
                {
                    LoggingStrategy = option;
                }
                else
                {
                    LoggingStrategy = LoggingStrategyOption.XFTool;
                }
                TraceEventTypeOption severity;
                if (Enum.TryParse <TraceEventTypeOption>(XFConstants.Config.DefaultPublishingSeverity, true, out severity))
                {
                    LoggingSeverity = severity;
                }
                else
                {
                    LoggingSeverity = TraceEventTypeOption.Verbose;
                }
                Infer        = false;
                LogSource    = XFConstants.Config.DefaultLogSource;
                ServiceToken = XFConstants.Config.DefaultServiceToken;

                ApiRoot    = XFConstants.Api.DefaultRoot;
                ApiErrors  = XFConstants.Api.DefaultErrorsEndpoint;
                ApiEvents  = XFConstants.Api.DefaultEventsEndpoint;
                ApiStatii  = XFConstants.Api.DefaultStatiiEndpoint;
                ApiMetrics = XFConstants.Api.DefaultMetricsEndpoint;
                ApiAlerts  = XFConstants.Api.DefaultAlertsEndpoint;
                ApiTasks   = XFConstants.Api.DefaultTasksEndpoint;
                ApiKpi     = XFConstants.Api.DefaultKpiEndpoint;
                ApiCustom  = XFConstants.Api.DefaultCustomEndpoint;
            }
            finally
            {
            }
            _ExtendedProperties.Add(XFConstants.Context.ZONE, Zone);
            _ExtendedProperties.Add(XFConstants.Context.Application, Context);
            _ExtendedProperties.Add(XFConstants.Context.INSTANCEIDENTIFIER, InstanceIdentifier);
        }
        static eXtensibleWebApiConfig()
        {
            IEventWriter  writer  = new EventLogWriter();
            StringBuilder message = new StringBuilder();
            var           props   = eXtensibleConfig.GetProperties();

            try
            {
                string configfilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                string configFolder   = Path.GetDirectoryName(configfilepath);

                WebApiPlugins = configFolder + "\\" + "webApiControllers";

                DateTimeSchemaOption  loggingSchema;
                LoggingModeOption     loggingMode;
                LoggingStrategyOption loggingStrategy;
                string logSchemaCandidate        = ConfigurationProvider.AppSettings[XFWebApiConstants.Config.LoggingSchema];
                string logToCandidate            = ConfigurationProvider.AppSettings[XFWebApiConstants.Config.LogToKey];
                string loggingModeCandidate      = ConfigurationProvider.AppSettings[XFWebApiConstants.Config.LoggingModeKey];
                string sqlConnectionKeyCandidate = ConfigurationProvider.AppSettings[XFWebApiConstants.Config.SqlConnectionKey];

                var configfilemap = new ExeConfigurationFileMap()
                {
                    ExeConfigFilename = configfilepath
                };
                Configuration           config  = ConfigurationManager.OpenMappedExeConfiguration(configfilemap, ConfigurationUserLevel.None);
                eXtensibleWebApiSection section = config.Sections[XFWebApiConstants.Config.SectionName] as eXtensibleWebApiSection;
                message.AppendLine(String.Format("{0}: {1}", "configFolder", configFolder));

                if (section != null)
                {
                    MessageProviderFolder = section.MessageProviderFolder;
                    var found = section.Elements.GetForLoggingMode(section.LoggingKey);
                    if (found != null)
                    {
                        message.AppendLine(String.Format("{0}: {1}", XFWebApiConstants.Config.SectionName, "found"));
                    }

                    if (Enum.TryParse <LoggingStrategyOption>(found.LoggingStrategy, true, out loggingStrategy) &&
                        Enum.TryParse <LoggingModeOption>(found.LoggingMode, true, out loggingMode))
                    {
                        if (loggingStrategy == LoggingStrategyOption.Datastore)
                        {
                            SqlConnectionKey = found.DatastoreKey;
                        }
                        LoggingMode = loggingMode;
                        LogTo       = loggingStrategy;
                        if (!String.IsNullOrEmpty(found.LoggingSchema) &&
                            Enum.TryParse <DateTimeSchemaOption>(found.LoggingSchema, true, out loggingSchema))
                        {
                            LoggingSchema = loggingSchema;
                        }

                        message.AppendLine(String.Format("{0}: {1}", "parsing", "success"));
                    }
                    else
                    {
                        loggingMode = XFWebApiConstants.Default.LoggingMode;
                        LogTo       = XFWebApiConstants.Default.LogTo;
                        message.AppendLine(String.Format("{0}: {1}", "parsing", "failure"));
                    }

                    IsEditRegistration = section.EditRegistration;
                    CatchAll           = section.CatchAllControllerId;
                    MessageIdHeaderKey = section.MessageIdHeaderKey;
                }
                else
                {
                    IsEditRegistration = false;
                    if (!String.IsNullOrWhiteSpace(loggingModeCandidate) && Enum.TryParse <LoggingModeOption>(loggingModeCandidate, true, out loggingMode))
                    {
                        LoggingMode = loggingMode;
                        message.AppendLine(String.Format("{0}: {1}", "parse LoggingMode", true));
                    }
                    else
                    {
                        LoggingMode = XFWebApiConstants.Default.LoggingMode;
                        message.AppendLine(String.Format("{0}: {1}", "parse LoggingMode", false));
                    }
                    // read from appsettings or use defaults
                    if (!String.IsNullOrWhiteSpace(logToCandidate) && Enum.TryParse <LoggingStrategyOption>(logToCandidate, true, out loggingStrategy))
                    {
                        LogTo = loggingStrategy;
                        message.AppendLine(String.Format("{0}: {1}", "parse LoggingTo", true));
                    }
                    else
                    {
                        LogTo = XFWebApiConstants.Default.LogTo;
                        message.AppendLine(String.Format("{0}: {1}", "parse LoggingTo", false));
                    }

                    if (!String.IsNullOrWhiteSpace(sqlConnectionKeyCandidate))
                    {
                        SqlConnectionKey = sqlConnectionKeyCandidate;
                        message.AppendLine(String.Format("{0}: {1}", "key", SqlConnectionKey));
                    }
                    else
                    {
                        SqlConnectionKey = XFWebApiConstants.Default.DatastoreConnectionKey;
                        message.AppendLine(String.Format("{0}: {1}", "key", "default"));
                    }

                    if (!String.IsNullOrWhiteSpace(logSchemaCandidate) &&
                        Enum.TryParse <DateTimeSchemaOption>(logSchemaCandidate, true, out loggingSchema))
                    {
                        LoggingSchema = loggingSchema;
                    }
                    else
                    {
                        LoggingSchema = DateTimeSchemaOption.None;
                    }

                    if (LogTo.Equals(LoggingStrategyOption.Datastore))
                    {
                        try
                        {
                            string cnText = ConfigurationProvider.ConnectionStrings[SqlConnectionKey].ConnectionString;
                            using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnText))
                            {
                                cn.Open();
                                message.AppendLine(String.Format("{0}: {1}", "cnState", cn.State));

                                if (cn.State == System.Data.ConnectionState.Open)
                                {
                                    IsLogToDatastore = true;
                                }
                                else
                                {
                                    LogTo = XFWebApiConstants.Default.LogTo;
                                    message.AppendLine(String.Format("{0}: {1}: {2}", "LogTo", "revert to default", LogTo));
                                }
                            }
                        }
                        catch (Exception innerEx)
                        {
                            LogTo = LoggingStrategyOption.None;
                            message.AppendLine(String.Format("{0}: {1}: {2}", "LogTo", "on error", LogTo));
                            string m = innerEx.InnerException != null ? innerEx.InnerException.Message : innerEx.Message;
                            message.AppendLine(m);
                        }
                    }
                    MessageProviderFolder = XFWebApiConstants.Default.MessageProviderFolder;
                    IsEditRegistration    = XFWebApiConstants.Default.IsEditRegistration;
                    CatchAll           = XFWebApiConstants.Default.CatchAllControllerId;
                    MessageIdHeaderKey = XFWebApiConstants.Default.MessageIdHeaderKey;
                }
            }
            catch (Exception ex)
            {
                // nows setup defaults
                MessageProviderFolder = XFWebApiConstants.Default.MessageProviderFolder;
                LoggingMode           = XFWebApiConstants.Default.LoggingMode;
                LogTo = XFWebApiConstants.Default.LogTo;
                IsEditRegistration = XFWebApiConstants.Default.IsEditRegistration;
                CatchAll           = XFWebApiConstants.Default.CatchAllControllerId;
                MessageIdHeaderKey = XFWebApiConstants.Default.MessageIdHeaderKey;
                var m = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                message.AppendLine(m);
            }
            //writer.WriteError(message.ToString(), SeverityType.Critical, "webApiconfig", props);
            //writer.WriteEvent(message.ToString(), "webApiConfig", props);
        }