public static LoggerConfiguration ELKBulk(this LoggerSinkConfiguration lc, SinkOptions options)
 {
     if (lc == null)
     {
         throw new ArgumentNullException(nameof(lc));
     }
     return(lc.Sink(new ELKSink(options), options.RestrictedToMinLevel));
 }
Example #2
0
 public ELKSink(SinkOptions options, bool includeDiagnostics = false)
     : base(options.BatchLimit, options.Period)
 {
     this.options = options;
     this.includeDiagnostics = includeDiagnostics;
 }
 public static LoggerConfiguration ELKBulk(this LoggerSinkConfiguration lc, SinkOptions options)
 {
     if (lc == null)
         throw new ArgumentNullException(nameof(lc));
     return lc.Sink(new ELKSink(options), options.RestrictedToMinLevel);
 }
 public ELKSink(SinkOptions options, bool includeDiagnostics = false)
     : base(options.BatchLimit, options.Period)
 {
     this.options            = options;
     this.includeDiagnostics = includeDiagnostics;
 }
Example #5
0
        public static string EventToJson(LogEvent logEvent, SinkOptions options)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }

            var payload = new Dictionary <string, object>();

            try
            {
                foreach (var kvp in logEvent.Properties)
                {
                    if (options.ShouldPropertyBeIgnored?.Invoke(kvp.Key) ?? false)
                    {
                        continue;
                    }

                    var safeKey = kvp.Key
                                  .Replace(" ", string.Empty)
                                  .Replace(":", string.Empty)
                                  .Replace("-", string.Empty)
                                  .Replace("_", string.Empty);

                    int dummy;
                    if (int.TryParse(kvp.Key, out dummy))
                    {
                        continue;
                    }
                    var simpleValue = SerilogPropertyFormatter.Simplify(kvp.Value);
                    payload[safeKey] = simpleValue;
                }

                payload["Level"]      = logEvent.Level.ToString();
                payload["@timestamp"] = logEvent.Timestamp;

                var message      = logEvent.RenderMessage();
                var messageBytes = Encoding.UTF8.GetBytes(message);

                if (messageBytes.Length > MAX_TERM_BYTES)
                {
                    var truncated = messageBytes.Length - MAX_TERM_BYTES;
                    var ending    = $"[truncated {truncated}]";
                    var subLength = MAX_TERM_BYTES - ending.Length;
                    if (subLength > 0)
                    {
                        message = message.Substring(0, subLength) + ending;
                        payload["@truncated"] = truncated;
                    }
                }

                payload["Message"] = message;

                if (logEvent.Exception != null)
                {
                    payload["Exception"] = logEvent.Exception.ToString();
                    var stackTrace = logEvent.Exception.StackTrace;
                    if (stackTrace != null)
                    {
                        stackTrace = StackTraceFilterRegexp.Replace(stackTrace, string.Empty);
                        payload["exc_stacktrace_hash"] = GetMurmur3HashString(stackTrace);
                    }
                }

                var result = JsonConvert.SerializeObject(payload,
                                                         new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });
                return(result);
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Error extracting json from logEvent {ex}");
            }
            return(null);
        }