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); }
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); }