Пример #1
0
        static void Main(string[] args)
        {
            _sourceValidators["DirectorySource"] = new DirectorySourceValidator();
            if (OperatingSystem.IsWindows())
            {
                _sourceValidators["WindowsEventLogSource"] = new EventLogValidator();
            }
            if (OperatingSystem.IsWindows())
            {
                _sourceValidators["WindowsPerformanceCounterSource"] = new PerformanceCounterValidator();
            }

            var exitCode = InvokeCommand(args);

            Environment.Exit(exitCode);
        }
Пример #2
0
        private bool LoadSources(IDictionary <String, String> sources, IConfigurationRoot config, IList <string> messages)
        {
            var sourcesSection = config.GetSection("Sources");
            var sourceSections = sourcesSection.GetChildren();
            var performanceCounterCategories = PerformanceCounterCategory.GetCategories();

            foreach (var sourceSection in sourceSections)
            {
                string id              = sourceSection["Id"];
                string sourceType      = sourceSection["SourceType"];
                string initialPosition = sourceSection["InitialPosition"];

                if (sourceType.Equals("DirectorySource"))
                {
                    string recordParser = sourceSection["RecordParser"];

                    if (recordParser.Equals("TimeStamp"))
                    {
                        string timestampFormat = sourceSection["TimestampFormat"];
                        if (string.IsNullOrEmpty(timestampFormat))
                        {
                            messages.Add($"Attribute 'TimestampFormat' is required in source ID: {id}.");
                            return(false);
                        }
                    }
                    else if (recordParser.Equals("Regex"))
                    {
                        string pattern = sourceSection["Pattern"];
                        if (string.IsNullOrEmpty(pattern))
                        {
                            messages.Add($"Attribute 'Pattern' is required in source ID: {id}.");
                            return(false);
                        }

                        string timestampFormat = sourceSection["TimestampFormat"];
                        if (string.IsNullOrEmpty(timestampFormat))
                        {
                            messages.Add($"Attribute 'TimestampFormat' is required in source ID: {id}.");
                            return(false);
                        }
                    }
                    else if (recordParser.Equals("Delimited"))
                    {
                        string delimiter       = sourceSection["Delimiter"];
                        string timestampField  = sourceSection["TimestampField"];
                        string timestampFormat = sourceSection["TimestampFormat"];

                        if (string.IsNullOrEmpty(delimiter))
                        {
                            messages.Add($"Attribute 'Delimiter' is required in source ID: {id}.");
                            return(false);
                        }

                        if (string.IsNullOrEmpty(timestampField))
                        {
                            messages.Add($"Attribute 'TimestampField' is required in source ID: {id}.");
                            return(false);
                        }

                        if (string.IsNullOrEmpty(timestampFormat))
                        {
                            messages.Add($"Attribute 'TimestampFormat' is required in source ID: {id}.");
                            return(false);
                        }
                    }
                }
                else if (sourceType.Equals("WindowsEventLogSource"))
                {
                    string            logName           = sourceSection["LogName"];
                    EventLogValidator eventLogValidator = new EventLogValidator();
                    if (!eventLogValidator.ValidateLogName(logName, messages))
                    {
                        return(false);
                    }
                }
                else if (sourceType.Equals("WindowsPerformanceCounterSource"))
                {
                    var categoriesSection = sourceSection.GetSection("Categories");
                    var validator         = new PerformanceCounterValidator(categoriesSection, performanceCounterCategories);
                    if (!validator.ValidateSource(messages))
                    {
                        return(false);
                    }
                }

                if (!string.IsNullOrEmpty(initialPosition) && initialPosition.Equals("Timestamp"))
                {
                    string initialPositionTimestamp = sourceSection["InitialPositionTimestamp"];
                    if (string.IsNullOrEmpty(initialPositionTimestamp))
                    {
                        messages.Add($"InitialPositionTimestamp required in source ID: {id}.");
                        return(false);
                    }
                    else
                    {
                        string timestampFormat = "yyyy-MM-dd HH:mm:ss.ffff";
                        if (!DateTime.TryParseExact(initialPositionTimestamp, timestampFormat, new CultureInfo("en-US"),
                                                    DateTimeStyles.None, out DateTime expectedDate))
                        {
                            messages.Add($"Timestamp doesn't match the DateTime format: {timestampFormat} in source {id}.");
                            return(false);
                        }
                    }
                }

                sources.Add(id, sourceType);
            }

            return(true);
        }