Example #1
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);
        }