Exemple #1
0
        public static string ToSchema(this DateTime now, DateTimeSchemaOption option, string defaultSchema = "log")
        {
            string s = !String.IsNullOrWhiteSpace(defaultSchema) ? defaultSchema.Trim() : "log";

            switch (option)
            {
            case DateTimeSchemaOption.MonthOfYear:
                s = now.Date.ToString("MMM");
                break;

            case DateTimeSchemaOption.WeekOfYear:
                s = now.Date.WeekOfYear().ToString("000");
                break;

            case DateTimeSchemaOption.DayOfWeek:
                s = now.DayOfWeek.ToString().Substring(0, 3);
                break;

            case DateTimeSchemaOption.DayOfYear:
                s = now.DayOfYear.ToString("000");
                break;

            case DateTimeSchemaOption.HourOfDay:
                s = now.ToString("HH");
                break;

            case DateTimeSchemaOption.HourOfDayOfWeek:
                s = String.Format("{0}{1}", now.ToString("HH"), (int)now.DayOfWeek);
                break;

            case DateTimeSchemaOption.None:
            default:
                break;
            }

            return(s.ToLower());
        }
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";
                ConfigurationProviderPlugins = configFolder + "\\" + "config.provider";
                BaseDirectory = configFolder;

                DateTimeSchemaOption loggingSchema;

                string     candidateContext  = ConfigurationManager.AppSettings[XFConstants.Application.Config.ApplicationKey];
                string     candidateZone     = ConfigurationManager.AppSettings[XFConstants.Application.Config.ZoneKey];
                string     candidateInstance = ConfigurationProvider.AppSettings[XFConstants.Application.Config.InstanceIdentifierKey];
                ZoneOption option;

                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 = ConfigurationProvider.AppSettings[XFConstants.Application.Config.LoggingStrategyKey];
                    //string candidateLoggingSeverity = ConfigurationProvider.AppSettings[XFConstants.Application.Config.LoggingSeverityKey];
                    string candidateConnectionStringKey = ConfigurationProvider.AppSettings[XFConstants.Application.Config.ConnectionStringKey];
                    string candidateLogSource           = ConfigurationProvider.AppSettings[XFConstants.Application.Config.LogSourceKey];
                    string logSchemaCandidate           = ConfigurationProvider.AppSettings[XFConstants.Application.Config.LoggingSchema];
                    string candidateBigDataUrl          = ConfigurationProvider.AppSettings[XFConstants.Application.Config.BigDataUrlKey];
                    string candidateServiceToken        = ConfigurationProvider.AppSettings[XFConstants.Application.Config.ServiceTokenKey];
                    //string candidateInstance = ConfigurationProvider.AppSettings[XFConstants.Application.Config.InstanceIdentifierKey];
                    string candidateInfer = ConfigurationProvider.AppSettings[XFConstants.Application.Config.InferKey];


                    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;
                    }

                    HandleAlerts = section.HandleAlerts;

                    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);
                    LogKey = !String.IsNullOrWhiteSpace(found.DatastoreKey) ? found.DatastoreKey : String.Empty;
                    LoggingStrategyOption loggingStrategy;
                    if (Enum.TryParse <LoggingStrategyOption>(found.LoggingStrategy, true, out loggingStrategy))
                    {
                        LoggingStrategy = loggingStrategy;
                        //inform = found.Inform;
                        if (!String.IsNullOrWhiteSpace(found.LoggingSchema) &&
                            Enum.TryParse <DateTimeSchemaOption>(found.LoggingSchema, true, out loggingSchema))
                        {
                            LoggingSchema = loggingSchema;
                        }
                    }
                    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
                {
                    if (!String.IsNullOrEmpty(candidateZone) && Enum.TryParse <ZoneOption>(candidateZone, true, out option))
                    {
                        Zone = option.ToString();
                    }
                    else
                    {
                        Zone = ZoneOption.Development.ToString();
                    }

                    Context            = !String.IsNullOrEmpty(candidateContext) ? candidateContext : XFConstants.Application.DefaultAppplicationKey;
                    InstanceIdentifier = (!String.IsNullOrEmpty(candidateInstance)) ? candidateInstance : Environment.MachineName.ToLower();

                    IsAsync                = section.IsAsync = false;
                    AppUserIdentity        = 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.Silent;
                }
                TraceEventTypeOption severity;
                if (Enum.TryParse <TraceEventTypeOption>(XFConstants.Config.DefaultPublishingSeverity, true, out severity))
                {
                    LoggingSeverity = severity;
                }
                else
                {
                    LoggingSeverity = TraceEventTypeOption.Verbose;
                }
                Infer        = false;
                Inform       = 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);
        }