Exemplo n.º 1
0
        private static FinalLogData ProcessLogItem(LogRequestSingleMsg logItem, LogRequestBase logRequestBase,
                                                   DateTime serverSideTimeUtc, JsnlogConfiguration jsnlogConfiguration)
        {
            string serversideLoggerNameOverride = jsnlogConfiguration.serverSideLogger;
            string messageFormat = jsnlogConfiguration.serverSideMessageFormat;
            string levelOverride = jsnlogConfiguration.serverSideLevel;
            string dateFormat    = jsnlogConfiguration.dateFormat;

            try
            {
                LevelUtils.ValidateLevel(levelOverride);
            }
            catch (Exception e)
            {
                throw new PropertyException("levelOverride", e);
            }

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

            string message = logItem.m;
            string logger  = logItem.n;
            string level   = logItem.l; // note that level as sent by the javascript is a number
            string entryId = logItem.u;

            DateTime utcDate     = DateTime.UtcNow;
            string   timestampMs = logItem.t;

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

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

            string jsonmessage = "";

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

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

            var logRequest       = new LogRequest(message, logger, level, utcDate, entryId, jsonmessage, logRequestBase);
            var loggingEventArgs = new LoggingEventArgs(logRequest)
            {
                Cancel = false,
                ServerSideMessageFormat = messageFormat
            };

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

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

            string consolidatedLevel = levelOverride ?? level;

            loggingEventArgs.FinalLevel = LevelUtils.ParseLevel(consolidatedLevel).Value;

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

            loggingEventArgs.FinalMessage = messageFormat
                                            .Replace("%message", message)
                                            .Replace("%entryId", entryId)
                                            .Replace("%jsonmessage", jsonmessage)
                                            .Replace("%utcDateServer", serverSideTimeUtc.ToString(dateFormat))
                                            .Replace("%utcDate", utcDate.ToString(dateFormat))
                                            .Replace("%dateServer", Utils.UtcToLocalDateTime(serverSideTimeUtc).ToString(dateFormat))
                                            .Replace("%date", Utils.UtcToLocalDateTime(utcDate).ToString(dateFormat))
                                            .Replace("%level", level)
                                            .Replace("%newline", System.Environment.NewLine)
                                            .Replace("%userAgent", logRequestBase.UserAgent)
                                            .Replace("%userHostAddress", logRequestBase.UserHostAddress)
                                            .Replace("%requestId", logRequestBase.RequestId ?? "")
                                            .Replace("%url", logRequestBase.Url)
                                            .Replace("%logger", logger);

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

            JavascriptLogging.RaiseLoggingEvent(loggingEventArgs);

            // If user wrote event handler that decided not to log the message, return null
            if (loggingEventArgs.Cancel)
            {
                return(null);
            }

            return(loggingEventArgs);
        }