Example #1
0
        private LogstashEvent GetJsonObject(LoggingEvent loggingEvent)
        {
            var obj = new LogstashEvent
            {
                version            = 1,
                timestamp          = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture),
                app                = App,
                source_host        = Environment.MachineName,
                thread_name        = loggingEvent.ThreadName,
                @class             = loggingEvent.LocationInformation.ClassName,
                method             = loggingEvent.LocationInformation.MethodName,
                line_number        = loggingEvent.LocationInformation.LineNumber,
                level              = loggingEvent.Level.ToString(),
                logger_name        = loggingEvent.LoggerName,
                message            = loggingEvent.RenderedMessage,
                propertyDictionary = loggingEvent.GetPropertyDictionary()
            };

            if (loggingEvent.ExceptionObject != null)
            {
                obj.exception = new LogstashException
                {
                    exception_class   = loggingEvent.ExceptionObject.GetType().ToString(),
                    exception_message = loggingEvent.ExceptionObject.Message,
                    stacktrace        = loggingEvent.ExceptionObject.StackTrace
                };
            }
            return(obj);
        }
        public static StringBuilder WriteMessage(this StringBuilder sb, LogstashEvent evt)
        {
            sb.WriteString(nameof(LogstashEvent.msg), evt.msg);

            if (evt.exception != null)
            {
                sb.Append(",")
                .Append("\"exception\":{")
                .WriteString(nameof(LogstashException.exception_class), evt.exception.exception_class).Append(",")
                .WriteString(nameof(LogstashException.exception_message), evt.exception.exception_message).Append(",")
                .WriteString(nameof(LogstashException.stacktrace), evt.exception.stacktrace)
                .Append("}");
            }
            return(sb);
        }
Example #3
0
        public static string ToJson(this LogstashEvent evt)
        {
            var logstash = new StringBuilder();

            logstash.Append("{")
            .WriteValueObject("@version", evt.version).Append(",")
            .WriteString("@timestamp", evt.timestamp).Append(",")
            .WriteString(nameof(LogstashEvent.source_host), evt.source_host).Append(",")
            .WriteString(nameof(LogstashEvent.app), evt.app).Append(",")
            .WriteString(nameof(LogstashEvent.thread_name), evt.thread_name).Append(",")
            .WriteString(nameof(LogstashEvent.@class), evt.@class).Append(",")
            .WriteString(nameof(LogstashEvent.method), evt.method).Append(",")
            .WriteString(nameof(LogstashEvent.line_number), evt.line_number).Append(",")
            .WriteString(nameof(LogstashEvent.level), evt.level).Append(",")
            .WriteString(nameof(LogstashEvent.logger_name), evt.logger_name).Append(",")
            .WriteMessage(evt)
            .Append("}");
            return(logstash.ToString());
        }