Ejemplo n.º 1
0
        private string CreateJsonFromLoggingEvent(log4net.Core.LoggingEvent loggingEvent, long sequence)
        {
            Dictionary<string, string> additionalFields;
            if (innerAdditionalFields != null)
                additionalFields = new Dictionary<string, string>(innerAdditionalFields);
            else
                additionalFields = new Dictionary<string, string>();

            var message = RenderLoggingEvent(loggingEvent);
            string fullMessage = message;

            int messagePos = message.IndexOf("Message:");
            if (messagePos > -1)
            {
                // Special format
                string otherProperties = message.Substring(0, messagePos);
                fullMessage = message.Substring(messagePos + 8);

                var extraFields = new Dictionary<string, string>();
                foreach (var part in otherProperties.Split(','))
                {
                    if (string.IsNullOrEmpty(part))
                        continue;
                    var keyValue = part.Split(':');
                    if (keyValue.Length > 1)
                        extraFields.Add(keyValue[0], keyValue[1]);
                }

                foreach (var extraField in extraFields)
                    additionalFields.Add(extraField.Key, extraField.Value);
            }

            if (loggingEvent.ExceptionObject != null)
            {
                fullMessage = String.Format("{0} - {1}. {2}. {3}.", fullMessage, loggingEvent.ExceptionObject.Source, loggingEvent.ExceptionObject.Message, loggingEvent.ExceptionObject.StackTrace);
            }

            if (StripEmptyMessages && string.IsNullOrEmpty(fullMessage))
                return null;

            var jsonMessage = new JsonMessage
            {
                Facility = (this.Facility ?? "LOG4NET"),
                File = "",
                FullMessage = fullMessage,
                Host = LoggingHostName,
                Level = loggingEvent.Level.ToString(),
                Line = "",
                Timestamp = loggingEvent.TimeStamp,
                Sequence = sequence
            };

            var dateInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo;
            jsonMessage.Index = string.Format(IndexId, jsonMessage.Timestamp,
                dateInfo.Calendar.GetWeekOfYear(jsonMessage.Timestamp, dateInfo.CalendarWeekRule, dateInfo.FirstDayOfWeek));

            //only set location information if configured
            if (IncludeLocationInformation)
            {
                jsonMessage.File = loggingEvent.LocationInformation.FileName;
                jsonMessage.Line = loggingEvent.LocationInformation.LineNumber;
            }

            //add additional fields and prepend with _ if not present already
            if (additionalFields != null)
            {
                foreach (var item in additionalFields)
                {
                    AddAdditionalFields(item.Key, item.Value, jsonMessage);
                }
            }
            AddAdditionalFields("appender-version", version, jsonMessage);

            //add additional fields and prepend with _ if not present already
            if (loggingEvent.Properties != null)
            {
                foreach (DictionaryEntry item in loggingEvent.Properties)
                {
                    var key = item.Key as string;
                    if (key != null)
                    {
                        AddAdditionalFields(key, item.Value as string, jsonMessage);
                    }
                }
            }

            return jsonMessage.GetJSON();
        }
Ejemplo n.º 2
0
        private string CreateJsonFromLoggingEvent(log4net.Core.LoggingEvent loggingEvent, long sequence)
        {
            Dictionary <string, string> additionalFields;

            if (innerAdditionalFields != null)
            {
                additionalFields = new Dictionary <string, string>(innerAdditionalFields);
            }
            else
            {
                additionalFields = new Dictionary <string, string>();
            }

            var    message     = RenderLoggingEvent(loggingEvent);
            string fullMessage = message;

            int messagePos = message.IndexOf("Message:");

            if (messagePos > -1)
            {
                // Special format
                string otherProperties = message.Substring(0, messagePos);
                fullMessage = message.Substring(messagePos + 8);

                var extraFields = new Dictionary <string, string>();
                foreach (var part in otherProperties.Split(','))
                {
                    if (string.IsNullOrEmpty(part))
                    {
                        continue;
                    }
                    var keyValue = part.Split(':');
                    if (keyValue.Length > 1)
                    {
                        extraFields.Add(keyValue[0], keyValue[1]);
                    }
                }

                foreach (var extraField in extraFields)
                {
                    additionalFields.Add(extraField.Key, extraField.Value);
                }
            }


            if (loggingEvent.ExceptionObject != null)
            {
                fullMessage = String.Format("{0} - {1}. {2}. {3}.", fullMessage, loggingEvent.ExceptionObject.Source, loggingEvent.ExceptionObject.Message, loggingEvent.ExceptionObject.StackTrace);
            }

            if (StripEmptyMessages && string.IsNullOrEmpty(fullMessage))
            {
                return(null);
            }

            var jsonMessage = new JsonMessage
            {
                Facility    = (this.Facility ?? "LOG4NET"),
                File        = "",
                FullMessage = fullMessage,
                Host        = LoggingHostName,
                Level       = loggingEvent.Level.ToString(),
                Line        = "",
                Timestamp   = loggingEvent.TimeStamp,
                Sequence    = sequence
            };

            var dateInfo = System.Globalization.DateTimeFormatInfo.CurrentInfo;

            jsonMessage.Index = string.Format(IndexId, jsonMessage.Timestamp,
                                              dateInfo.Calendar.GetWeekOfYear(jsonMessage.Timestamp, dateInfo.CalendarWeekRule, dateInfo.FirstDayOfWeek));

            //only set location information if configured
            if (IncludeLocationInformation)
            {
                jsonMessage.File = loggingEvent.LocationInformation.FileName;
                jsonMessage.Line = loggingEvent.LocationInformation.LineNumber;
            }

            //add additional fields and prepend with _ if not present already
            if (additionalFields != null)
            {
                foreach (var item in additionalFields)
                {
                    AddAdditionalFields(item.Key, item.Value, jsonMessage);
                }
            }
            AddAdditionalFields("appender-version", version, jsonMessage);

            //add additional fields and prepend with _ if not present already
            if (loggingEvent.Properties != null)
            {
                foreach (DictionaryEntry item in loggingEvent.Properties)
                {
                    var key = item.Key as string;
                    if (key != null)
                    {
                        AddAdditionalFields(key, item.Value as string, jsonMessage);
                    }
                }
            }

            return(jsonMessage.GetJSON());
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Add    
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="json"></param>
 private void AddAdditionalFields(string key, string value, JsonMessage message)
 {
     if (key != null)
     {
         key = Regex.Replace(key, "[\\W]", "");
         message.AdditionalFields.Add(new KeyValuePair<string, string>(key, value));
     }
 }