Пример #1
0
        private string CreateFatalGelfJson(Exception exception)
        {
            var gelfMessage = new GelfMessage
            {
                Facility     = Facility ?? "GELF",
                FullMessage  = "Error sending message in NLog.Targets.Gelf",
                Host         = Dns.GetHostName(),
                Level        = LogLevel.Fatal.GelfSeverity(),
                ShortMessage = "Error sending message in NLog.Targets.Gelf"
            };

            if (exception == null)
            {
                return(JsonConvert.SerializeObject(gelfMessage));
            }

            var exceptioToLog = exception;

            while (exceptioToLog.InnerException != null)
            {
                exceptioToLog = exceptioToLog.InnerException;
            }

            gelfMessage.ExceptionType    = exceptioToLog.GetType().Name;
            gelfMessage.ExceptionMessage = exceptioToLog.Message;
            gelfMessage.StackTrace       = exceptioToLog.StackTrace;

            return(JsonConvert.SerializeObject(gelfMessage));
        }
        private string CreateGelfJsonFromLoggingEvent(LogEventInfo logEventInfo)
        {
            string shortMessage = null;

            if (logEventInfo.FormattedMessage != null)
            {
                shortMessage = logEventInfo.FormattedMessage.Length > ShortMessageLength
                    ? logEventInfo.FormattedMessage.Substring(0, ShortMessageLength - 1)
                    : logEventInfo.FormattedMessage;
            }

            var gelfMessage = new GelfMessage
            {
                Facility     = Facility ?? "GELF",
                Version      = Version,
                FullMessage  = logEventInfo.FormattedMessage,
                Host         = HostName,
                Level        = logEventInfo.Level.GelfSeverity(),
                ShortMessage = shortMessage,
                Logger       = logEventInfo.LoggerName ?? ""
            };

            if (Activity.Current?.RootId != null)
            {
                gelfMessage.RequestId = Activity.Current?.RootId;
            }

            if (logEventInfo.Properties != null)
            {
                object notes;
                if (logEventInfo.Properties.TryGetValue("Notes", out notes))
                {
                    gelfMessage.Notes = (string)notes;
                }
            }

            if (logEventInfo.Exception == null)
            {
                return(JsonConvert.SerializeObject(gelfMessage, JsonSerializerSettings));
            }

            var exceptionToLog = logEventInfo.Exception;

            while (exceptionToLog.InnerException != null)
            {
                exceptionToLog = exceptionToLog.InnerException;
            }

            gelfMessage.ExceptionType    = exceptionToLog.GetType().Name;
            gelfMessage.ExceptionMessage = exceptionToLog.Message;
            gelfMessage.StackTrace       = exceptionToLog.StackTrace;

            return(JsonConvert.SerializeObject(gelfMessage, JsonSerializerSettings));
        }
        private GelfMessage CreateGelfJsonFromLoggingEvent(LogEventInfo logEventInfo)
        {
            var shortMessage = logEventInfo.FormattedMessage.Length > ShortMessageLength?logEventInfo.FormattedMessage.Substring(0, ShortMessageLength - 1) : logEventInfo.FormattedMessage;

            var gelfMessage = new GelfMessage
            {
                Facility     = Facility ?? "GELF",
                FullMessage  = logEventInfo.FormattedMessage,
                Host         = Dns.GetHostName(),
                Level        = logEventInfo.Level.GelfSeverity(),
                ShortMessage = shortMessage
            };

            if (!string.IsNullOrWhiteSpace(logEventInfo.LoggerName))
            {
                gelfMessage.Add("Logger", logEventInfo.LoggerName);
            }

            if (logEventInfo.Properties != null)
            {
                object notes;
                if (logEventInfo.Properties.TryGetValue("Notes", out notes))
                {
                    gelfMessage.Add("Notes", notes);
                }
            }

            if (logEventInfo.Exception == null)
            {
                return(gelfMessage);
            }

            var exceptioToLog = logEventInfo.Exception;

            gelfMessage.Add("ExceptionType", exceptioToLog.GetType().Name);
            gelfMessage.Add("ExceptionMessage", exceptioToLog.Message);
            gelfMessage.Add("Exception", exceptioToLog.ToString());

            return(gelfMessage);
        }
        private GelfMessage CreateFatalGelfJson(Exception exception)
        {
            var gelfMessage = new GelfMessage
            {
                Facility     = Facility ?? "GELF",
                FullMessage  = "Error sending message in NLog.Targets.Gelf",
                Host         = Dns.GetHostName(),
                Level        = LogLevel.Fatal.GelfSeverity(),
                ShortMessage = "Error sending message in NLog.Targets.Gelf"
            };

            if (exception == null)
            {
                return(gelfMessage);
            }

            var exceptioToLog = exception;

            gelfMessage.Add("ExceptionType", exceptioToLog.GetType().Name);
            gelfMessage.Add("ExceptionMessage", exceptioToLog.Message);
            gelfMessage.Add("Exception", exceptioToLog.ToString());

            return(gelfMessage);
        }
Пример #5
0
        public JObject GetGelfJson(LogEventInfo logEventInfo, Layout layout, string facility)
        {
            //Retrieve the formatted message from LogEventInfo
            var logEventMessage = layout.Render(logEventInfo);

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

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

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

            var dateTime     = logEventInfo.TimeStamp;
            var epoch        = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var unixDateTime = (dateTime.ToUniversalTime() - epoch).TotalSeconds + (dateTime.Millisecond / 1000d);

            //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    = unixDateTime,
                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);
        }
Пример #6
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)
            {
                string exceptionDetail;
                string stackDetail;

                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 http://docs.graylog.org/en/2.4/pages/gelf.html#gelf-payload-specification
            //Version 1.1 (11/2013)
            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);
        }
        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)
            {
                string exceptionDetail;
                string stackDetail; 

                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;
        }
        private string CreateFatalGelfJson(Exception exception)
        {
            var gelfMessage = new GelfMessage
                {
                    Facility = Facility ?? "GELF",
                    FullMessage = "Error sending message in NLog.Targets.Gelf",
                    Host = Dns.GetHostName(),
                    Level = LogLevel.Fatal.GelfSeverity(),
                    ShortMessage = "Error sending message in NLog.Targets.Gelf"
                };

            if (exception == null) return JsonConvert.SerializeObject(gelfMessage);

            var exceptioToLog = exception;

            while (exceptioToLog.InnerException != null)
            {
                exceptioToLog = exceptioToLog.InnerException;
            }

            gelfMessage.ExceptionType = exceptioToLog.GetType().Name;
            gelfMessage.ExceptionMessage = exceptioToLog.Message;
            gelfMessage.StackTrace = exceptioToLog.StackTrace;

            return JsonConvert.SerializeObject(gelfMessage);
        }
        private string CreateGelfJsonFromLoggingEvent(LogEventInfo logEventInfo)
        {
            var shortMessage = logEventInfo.FormattedMessage.Length > ShortMessageLength ? logEventInfo.FormattedMessage.Substring(0, ShortMessageLength - 1) : logEventInfo.FormattedMessage;

            var gelfMessage = new GelfMessage
                {
                    Facility = Facility ?? "GELF",
                    FullMessage = logEventInfo.FormattedMessage,
                    Host = Dns.GetHostName(),
                    Level = logEventInfo.Level.GelfSeverity(),
                    ShortMessage = shortMessage,
                    Logger = logEventInfo.LoggerName ?? ""
                };

            if (logEventInfo.Properties != null)
            {
                object notes;
                if (logEventInfo.Properties.TryGetValue("Notes", out notes))
                {
                    gelfMessage.Notes = (string) notes;
                }
            }

            if (logEventInfo.Exception == null) return JsonConvert.SerializeObject(gelfMessage);

            var exceptioToLog = logEventInfo.Exception;

            while (exceptioToLog.InnerException != null)
            {
                exceptioToLog = exceptioToLog.InnerException;
            }

            gelfMessage.ExceptionType = exceptioToLog.GetType().Name;
            gelfMessage.ExceptionMessage = exceptioToLog.Message;
            gelfMessage.StackTrace = exceptioToLog.StackTrace;

            return JsonConvert.SerializeObject(gelfMessage);
        }
        private void SendMessage(string gelfServer, int serverPort, GelfMessage message)
        {
            var publisher = new GelfPublisher(gelfServer, serverPort);

            publisher.Publish(message);
        }