コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }