Beispiel #1
0
        public JObject GetGelfJson(LogEventInfo logEventInfo, string facility)
        {
            //Retrieve the formatted message from LogEventInfo
            var logEventMessage = logEventInfo.FormattedMessage;
            if (logEventMessage == null) return null;

            //If we are dealing with an exception, pass exception properties to LogEventInfo properties
            if (logEventInfo.Exception != null)
            {
                var exceptionDetail = string.Empty;
                string stackDetail = null;

                this.GetExceptionMessages(logEventInfo.Exception, out exceptionDetail, out stackDetail);
                logEventInfo.Properties.Add("ExceptionSource", logEventInfo.Exception.Source);
                logEventInfo.Properties.Add("ExceptionMessage", exceptionDetail);
                logEventInfo.Properties.Add("StackTrace", stackDetail);
            }

            //Figure out the short message
            var shortMessage = logEventMessage;
            if (shortMessage.Length > ShortMessageMaxLength)
            {
                shortMessage = shortMessage.Substring(0, ShortMessageMaxLength);
            }

            //Construct the instance of GelfMessage
            //See https://github.com/Graylog2/graylog2-docs/wiki/GELF "Specification (version 1.0)"
            var gelfMessage = new GelfMessage
                                  {
                                      Version = GelfVersion,
                                      Host = Dns.GetHostName(),
                                      ShortMessage = shortMessage,
                                      FullMessage = logEventMessage,
                                      Timestamp = logEventInfo.TimeStamp,
                                      Level = GetSeverityLevel(logEventInfo.Level),
                                      //Spec says: facility must be set by the client to "GELF" if empty
                                      Facility = (string.IsNullOrEmpty(facility) ? "GELF" : facility),
                                      Line = (logEventInfo.UserStackFrame != null)
                                                 ? logEventInfo.UserStackFrame.GetFileLineNumber().ToString(
                                                     CultureInfo.InvariantCulture)
                                                 : string.Empty,
                                      File = (logEventInfo.UserStackFrame != null)
                                                 ? logEventInfo.UserStackFrame.GetFileName()
                                                 : string.Empty,
                                  };

            //Convert to JSON
            var jsonObject = JObject.FromObject(gelfMessage);

            //Add any other interesting data to LogEventInfo properties
            logEventInfo.Properties.Add("LoggerName", logEventInfo.LoggerName);

            //We will persist them "Additional Fields" according to Gelf spec
            foreach (var property in logEventInfo.Properties)
            {
                AddAdditionalField(jsonObject, property);
            }

            return jsonObject;
        }
Beispiel #2
0
        public JObject GetGelfJson(LogEventInfo logEventInfo, string application, string environment, IList <RedactInfo> redactions)
        {
            //Retrieve the formatted message from LogEventInfo
            var logEventMessage = redactions.Aggregate(logEventInfo.FormattedMessage,
                                                       (current, redaction) => redaction.LazyRegex.Value.Replace(current, redaction.Replacement));

            if (logEventMessage == null)
            {
                return(null);
            }

            //Construct the instance of GelfMessage
            //See http://docs.graylog.org/en/2.1/pages/gelf.html?highlight=short%20message#gelf-format-specification "Specification (version 1.1)"
            var gelfMessage = new GelfMessage
            {
                Version      = GelfVersion,
                Host         = Dns.GetHostName().ToUpper(),
                ShortMessage = GetShortMessage(logEventMessage),
                FullMessage  = logEventMessage,
                Timestamp    = logEventInfo.TimeStamp,
                Level        = GetSeverityLevel(logEventInfo.Level)
            };

            //Convert to JSON
            var jsonObject = JObject.FromObject(gelfMessage);

            //Add any other interesting data to additional fields
            AddAdditionalField(jsonObject, new KeyValuePair <object, object>("application", application));
            AddAdditionalField(jsonObject, new KeyValuePair <object, object>("environment", environment));
            AddAdditionalField(jsonObject, new KeyValuePair <object, object>("line", logEventInfo.UserStackFrame?.GetFileLineNumber().ToString(CultureInfo.InvariantCulture)));
            AddAdditionalField(jsonObject, new KeyValuePair <object, object>("file", logEventInfo.UserStackFrame?.GetFileName()));
            AddAdditionalField(jsonObject, new KeyValuePair <object, object>("LoggerName", logEventInfo.LoggerName));
            AddAdditionalField(jsonObject, new KeyValuePair <object, object>("LogLevelName", logEventInfo.Level?.ToString()));

            //If we are dealing with an exception, add exception properties as additional fields
            if (logEventInfo.Exception != null)
            {
                string exceptionDetail;
                string stackDetail;

                GetExceptionMessages(logEventInfo.Exception, out exceptionDetail, out stackDetail);

                AddAdditionalField(jsonObject, new KeyValuePair <object, object>("ExceptionSource", logEventInfo.Exception.Source));
                AddAdditionalField(jsonObject, new KeyValuePair <object, object>("ExceptionMessage", exceptionDetail));
                AddAdditionalField(jsonObject, new KeyValuePair <object, object>("StackTrace", stackDetail));
            }

            foreach (var property in logEventInfo.Properties)
            {
                AddAdditionalField(jsonObject, property);
            }

            return(jsonObject);
        }
Beispiel #3
0
        public JObject GetGelfJson(LogEventInfo logEventInfo, string facility)
        {
            //Retrieve the formatted message from LogEventInfo
            var logEventMessage = logEventInfo.FormattedMessage;

            if (logEventMessage == null)
            {
                return(null);
            }

            //If we are dealing with an exception, pass exception properties to LogEventInfo properties
            if (logEventInfo.Exception != null)
            {
                logEventInfo.Properties.Add("ExceptionSource", logEventInfo.Exception.Source);
                logEventInfo.Properties.Add("ExceptionMessage", logEventInfo.Exception.Message);
                logEventInfo.Properties.Add("StackTrace", logEventInfo.Exception.StackTrace);
            }

            //Figure out the short message
            var shortMessage = logEventMessage;

            if (shortMessage.Length > ShortMessageMaxLength)
            {
                shortMessage = shortMessage.Substring(0, ShortMessageMaxLength);
            }

            //Construct the instance of GelfMessage
            //See https://github.com/Graylog2/graylog2-docs/wiki/GELF "Specification (version 1.0)"
            var gelfMessage = new GelfMessage
            {
                Version      = GelfVersion,
                Host         = Dns.GetHostName(),
                ShortMessage = shortMessage,
                FullMessage  = logEventMessage,
                Timestamp    = logEventInfo.TimeStamp,
                Level        = GetSeverityLevel(logEventInfo.Level),
                //Spec says: facility must be set by the client to "GELF" if empty
                Facility = (string.IsNullOrEmpty(facility) ? "GELF" : facility),
                Line     = (logEventInfo.UserStackFrame != null)
                                                 ? logEventInfo.UserStackFrame.GetFileLineNumber().ToString(
                    CultureInfo.InvariantCulture)
                                                 : string.Empty,
                File = (logEventInfo.UserStackFrame != null)
                                                 ? logEventInfo.UserStackFrame.GetFileName()
                                                 : string.Empty,
            };

            //Convert to JSON
            var jsonObject = JObject.FromObject(gelfMessage);

            //Add any other interesting data to LogEventInfo properties
            logEventInfo.Properties.Add("LoggerName", logEventInfo.LoggerName);

            //We will persist them "Additional Fields" according to Gelf spec
            foreach (var property in logEventInfo.Properties)
            {
                AddAdditionalField(jsonObject, property);
            }

            return(jsonObject);
        }