Exemplo n.º 1
0
        public void Validate(string appenderName, AttributeValueCollection attributeValues)
        {
            // Ensure that if any of the buffer specific attributes are provided, they are all provided, and that they make sense.

            if (attributeValues.ContainsKey(Constants.AttributeNameSendWithBufferLevel) ||
                attributeValues.ContainsKey(Constants.AttributeNameStoreInBufferLevel) ||
                attributeValues.ContainsKey(Constants.AttributeNameBufferSize))
            {
                if ((!attributeValues.ContainsKey(Constants.AttributeNameSendWithBufferLevel)) ||
                    (!attributeValues.ContainsKey(Constants.AttributeNameStoreInBufferLevel)) ||
                    (!attributeValues.ContainsKey(Constants.AttributeNameBufferSize)))
                {
                    throw new GeneralAppenderException(appenderName, string.Format(
                                                           "If any of {0}, {1} or {2} is specified, than the other two need to be specified as well",
                                                           Constants.AttributeNameSendWithBufferLevel, Constants.AttributeNameStoreInBufferLevel, Constants.AttributeNameBufferSize));
                }

                int level =
                    attributeValues.ContainsKey(Constants.AttributeNameLevel) ?
                    LevelUtils.LevelNumber(attributeValues[Constants.AttributeNameLevel].Text) :
                    (int)Constants.DefaultAppenderLevel;

                int storeInBufferLevel  = LevelUtils.LevelNumber(attributeValues[Constants.AttributeNameStoreInBufferLevel].Text);
                int sendWithBufferLevel = LevelUtils.LevelNumber(attributeValues[Constants.AttributeNameSendWithBufferLevel].Text);

                if ((storeInBufferLevel > level) || (level > sendWithBufferLevel))
                {
                    throw new GeneralAppenderException(appenderName, string.Format(
                                                           "{0} must be equal or greater than {1} and equal or smaller than {2}",
                                                           Constants.AttributeNameLevel, Constants.AttributeNameStoreInBufferLevel,
                                                           Constants.AttributeNameSendWithBufferLevel));
                }
            }
        }
Exemplo n.º 2
0
        private void ValidateAppender(string configurationObjectName)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new MissingAttributeException(configurationObjectName, FieldName);
            }

            if (maxBatchSize < batchSize)
            {
                throw new GeneralAppenderException(name,
                                                   string.Format("maxBatchSize ({0}) is smaller than batchSize ({1})", maxBatchSize, batchSize));
            }

            // Ensure that if any of the buffer specific attributes are provided, they are all provided, and that they make sense.

            bool levelGiven = !string.IsNullOrEmpty(level);
            bool sendWithBufferLevelGiven = !string.IsNullOrEmpty(sendWithBufferLevel);
            bool storeInBufferLevelGiven  = !string.IsNullOrEmpty(storeInBufferLevel);
            bool bufferSizeGiven          = (bufferSize > 0);

            if (sendWithBufferLevelGiven ||
                storeInBufferLevelGiven ||
                bufferSizeGiven)
            {
                if ((!sendWithBufferLevelGiven) ||
                    (!storeInBufferLevelGiven) ||
                    (!bufferSizeGiven))
                {
                    throw new GeneralAppenderException(name, string.Format(
                                                           "If any of {0}, {1} or {2} is specified, than the other two need to be specified as well",
                                                           FieldSendWithBufferLevel, FieldStoreInBufferLevel, FieldBufferSize));
                }

                int levelNumber =
                    levelGiven ?
                    LevelUtils.LevelNumber(level) :
                    (int)Constants.DefaultAppenderLevel;

                int storeInBufferLevelNumber  = LevelUtils.LevelNumber(storeInBufferLevel);
                int sendWithBufferLevelNumber = LevelUtils.LevelNumber(sendWithBufferLevel);

                if ((storeInBufferLevelNumber > levelNumber) || (levelNumber > sendWithBufferLevelNumber))
                {
                    throw new GeneralAppenderException(name, string.Format(
                                                           "{0} must be equal or greater than {1} and equal or smaller than {2}",
                                                           FieldLevel, FieldStoreInBufferLevel, FieldSendWithBufferLevel));
                }
            }
        }
Exemplo n.º 3
0
        private static LogData ProcessLogItem(Dictionary <string, Object> logItem, string userAgent, string userHostAddress,
                                              string requestId, DateTime serverSideTimeUtc, string url, XmlElement xe)
        {
            string serversideLoggerNameOverride = XmlHelpers.OptionalAttribute(xe, "serverSideLogger", null);
            string messageFormat = XmlHelpers.OptionalAttribute(xe, "serverSideMessageFormat", "%message");
            string levelOverride = XmlHelpers.OptionalAttribute(xe, "serverSideLevel", null, LevelUtils.LevelRegex());
            string dateFormat    = XmlHelpers.OptionalAttribute(xe, "dateFormat", "o");

            // ----------------

            string message = logItem["m"].ToString();
            string logger  = logItem["n"].ToString();
            string level   = logItem["l"].ToString(); // note that level as sent by the javascript is a number

            DateTime utcTimestamp = DateTime.UtcNow;
            string   timestampMs  = logItem["t"].ToString();

            try
            {
                double ms = double.Parse(timestampMs);
                utcTimestamp = DateTime.SpecifyKind((new DateTime(1970, 1, 1)).AddMilliseconds(ms), DateTimeKind.Utc);
            }
            catch
            {
            }

            // ----------------

            if (string.IsNullOrWhiteSpace(logger))
            {
                logger = Constants.RootLoggerNameServerSide;
            }
            string finalLoggerName = serversideLoggerNameOverride ?? logger;

            string finalLevel = levelOverride ?? level;

            // ----------------

            string jsonmessage = "";

            if (messageFormat.Contains("%jsonmessage"))
            {
                jsonmessage = LogMessageHelpers.EnsureValidJson(message);
            }

            // ----------------

            string finalMessage = messageFormat
                                  .Replace("%message", message)
                                  .Replace("%jsonmessage", jsonmessage)
                                  .Replace("%utcDateServer", serverSideTimeUtc.ToString(dateFormat))
                                  .Replace("%utcDate", utcTimestamp.ToString(dateFormat))
                                  .Replace("%dateServer", Utils.UtcToLocalDateTime(serverSideTimeUtc).ToString(dateFormat))
                                  .Replace("%date", Utils.UtcToLocalDateTime(utcTimestamp).ToString(dateFormat))
                                  .Replace("%level", level)
                                  .Replace("%newline", System.Environment.NewLine)
                                  .Replace("%userAgent", userAgent)
                                  .Replace("%userHostAddress", userHostAddress)
                                  .Replace("%requestId", requestId ?? "")
                                  .Replace("%url", url)
                                  .Replace("%logger", logger);

            // ---------------

            LogData logData = new LogData(
                finalMessage, finalLoggerName, LevelUtils.ParseLevel(finalLevel).Value, LevelUtils.LevelNumber(finalLevel),
                message, int.Parse(level), logger, requestId,
                utcTimestamp, serverSideTimeUtc, Utils.UtcToLocalDateTime(utcTimestamp), Utils.UtcToLocalDateTime(serverSideTimeUtc),
                userAgent, userHostAddress, url);

            return(logData);
        }
        public string ToJavaScript(string text)
        {
            string js = LevelUtils.LevelNumber(text).ToString();

            return(js);
        }