Exemplo n.º 1
0
        protected override void SendBuffer(LoggingEvent[] events)
        {
            if (events == null || events.Length == 0)
            {
                return;
            }

            var sb = new StringBuilder(4096);

            foreach (var le in events)
            {
                try
                {
                    sb.AppendLine("{ \"index\" : {} }");
                    var json = new
                    {
                        le.LoggerName,
                        Level      = le.Level.Name,
                        TimeStamp  = le.TimeStampUtc,
                        Message    = le.RenderedMessage,
                        Exception  = le.GetExceptionString(),
                        Properties = GetLoggingEventProperties(le).ToDictionary(),
                    }.ToJson(new Newtonsoft.Json.JsonSerializerSettings()
                    {
                        NullValueHandling     = Newtonsoft.Json.NullValueHandling.Ignore,
                        DateTimeZoneHandling  = Newtonsoft.Json.DateTimeZoneHandling.Utc,
                        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                        MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore,
                    });
                    sb.AppendLine(json);
                }
                catch (Exception ex)
                {
                    ErrorHandler.Error(ex.Message, ex);
                    InvokeHelper.OnInvokeException?.Invoke(ex);
                }
            }

            var url = $"{ElasticSearchUrl}/{IndexFormat.Replace("{applicationName}", ApplicationName.GetValueOrDefault(ApplicationHelper.ApplicationName).ToLower()).Replace("{rollingDate}", DateTime.UtcNow.ToString("yyyyMMdd"))}/{Type}/_bulk";

            try
            {
                _httpClient.PostAsync(url, new StringContent(sb.ToString(), Encoding.UTF8, "application/json"))
                .ContinueWith(_ => _.Result.Dispose()).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                ErrorHandler.Error(ex.Message, ex);
                InvokeHelper.OnInvokeException?.Invoke(ex);
            }
        }