/// <summary>
        /// Validate the full set of configuration. Throws InvalidConfigurationException in the event of invalid
        /// applied configuration.
        /// </summary>
        public void Validate()
        {
            if (this.Type == LogType.Network)
            {
                if (this.hostname == null)
                {
                    throw new InvalidConfigurationException($"Log {this.Name} has no hostname set.");
                }
                if (this.Port == 0)
                {
                    throw new InvalidConfigurationException($"Log {this.Name} has no port set.");
                }
            }

            if (this.Type.HasFeature(Features.FileBacked))
            {
                if (this.RotationInterval > 0 &&
                    !FileBackedLogger.IsValidFilenameTemplate(this.FilenameTemplate,
                                                              TimeSpan.FromSeconds(this.RotationInterval)))
                {
                    throw new InvalidConfigurationException(
                              $"Rotation interval {this.RotationInterval} cannot be used with template {this.FilenameTemplate}.");
                }

                var retentionPolicySet = (this.MaximumAge > TimeSpan.Zero || this.MaximumSize > 0);
                if (retentionPolicySet && this.RotationInterval <= 0)
                {
                    throw new InvalidConfigurationException(
                              $"Log {this.Name} has retention configuration but is not rotated.");
                }

                if (this.MaximumAge > TimeSpan.Zero)
                {
                    if (this.MaximumAge < TimeSpan.FromSeconds(this.RotationInterval) ||
                        this.MaximumAge.Ticks % TimeSpan.TicksPerSecond != 0)
                    {
                        throw new InvalidConfigurationException(
                                  $"Log {this.Name} has a maximum age which is either shorter than the rotation interval or not evenly divisible by seconds.");
                    }
                }
            }
        }
示例#2
0
        private static bool ParseLogNode(XmlNode xmlNode, LogConfiguration config)
        {
            bool clean = true;

            foreach (XmlAttribute logAttribute in xmlNode.Attributes)
            {
                switch (logAttribute.Name.ToLower(CultureInfo.InvariantCulture))
                {
                case LogBufferSizeAttribute:
                    if (!int.TryParse(logAttribute.Value, out config.BufferSize) ||
                        !IsValidFileBufferSize(config.BufferSize))
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid buffer size " + logAttribute.Value);
                        config.BufferSize = DefaultFileBufferSizeMB;
                        clean             = false;
                    }
                    break;

                case LogDirectoryAttribute:
                    if (!IsValidDirectory(logAttribute.Value))
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid directory name " + logAttribute.Value);
                        clean = false;
                    }
                    else
                    {
                        config.Directory = logAttribute.Value;
                    }
                    break;

                case LogFilenameTemplateAttribute:
                    if (!FileBackedLogger.IsValidFilenameTemplate(logAttribute.Value))
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid filename template " + logAttribute.Value);
                        clean = false;
                    }
                    else
                    {
                        config.FilenameTemplate = logAttribute.Value;
                    }
                    break;

                case LogTimestampLocal:
                    if (!bool.TryParse(logAttribute.Value, out config.TimestampLocal))
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid timestamplocal value " + logAttribute.Value);
                        config.TimestampLocal = false;
                        clean = false;
                    }
                    break;

                case LogRotationAttribute:
                    if (!int.TryParse(logAttribute.Value, out config.RotationInterval) ||
                        !IsValidRotationInterval(config.RotationInterval))
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid rotation interval " + logAttribute.Value);
                        config.RotationInterval = DefaultRotationInterval;
                        clean = false;
                    }
                    break;

                case LogHostnameAttribute:
                    if (Uri.CheckHostName(logAttribute.Value) == UriHostNameType.Unknown)
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid hostname name " + logAttribute.Value);
                        clean = false;
                    }
                    else
                    {
                        config.Hostname = logAttribute.Value;
                    }
                    break;

                case LogPortAttribute:
                    if (!int.TryParse(logAttribute.Value, out config.Port) ||
                        config.Port <= 0)
                    {
                        InternalLogger.Write.InvalidConfiguration("invalid port " + logAttribute.Value);
                        config.Port = 80;
                        clean       = false;
                    }
                    break;
                }
            }

            clean &= ParseLogSources(xmlNode, config);
            clean &= ParseLogFilters(xmlNode, config);
            return(clean);
        }