private string Serialize(LogEvent logEvent)
        {
            var s = new StringWriter();

            _formatter.Format(logEvent, s);
            return(s.ToString());
        }
Пример #2
0
        public IWebHostBuilder CreateWebHostBuilder(TextWriter inputWriter) =>

        // Task.Run(() => HeartbeatAsync(inputWriter), _cancel.Token)

        WebHost.CreateDefaultBuilder()
        .UseUrls($"http://localhost:{Port}")
        .UseKestrel()
        .Configure(app =>
        {
            app.Run(async(context) =>
            {
                try
                {
                    var octopusEvent = await parsers.ParseOctopusEvent(context.Request);
                    if (octopusEvent?.Payload == null)
                    {
                        context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    }
                    else
                    {
                        var seqEvent = parsers.ParseSeqEvent(octopusEvent);

                        formatter.Format(seqEvent, inputWriter);

                        context.Response.StatusCode = StatusCodes.Status202Accepted;
                    }
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                    await context.Response.WriteAsync(ex.Message);
                }
            });
        });
Пример #3
0
        private void Configure(IApplicationBuilder app)
        {
            var formatter = new CompactJsonFormatter();

            app.Run(
                ctx =>
            {
                if (ctx.Request.Method == "GET")
                {
                    ctx.Response.StatusCode  = 200;
                    ctx.Response.ContentType = "application/vnd.serilog.clef";

                    using (var writer = new StreamWriter(ctx.Response.Body, System.Text.Encoding.UTF8))
                    {
                        while (this.logEvents.TryPop(out var logEvent))
                        {
                            formatter.Format(logEvent, writer);
                        }
                    }

                    ctx.Response.Body.Flush();
                }
                else if (ctx.Request.Method == "DELETE")
                {
                    this.logEvents.Clear();
                }
                else
                {
                    ctx.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
                }

                return(Task.CompletedTask);
            });
        }
Пример #4
0
        public static async Task <int> ShipEvents(
            SeqConnection connection,
            LogEventReader reader,
            List <ILogEventEnricher> enrichers,
            InvalidDataHandling invalidDataHandling,
            Func <LogEvent, bool> filter = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (enrichers == null)
            {
                throw new ArgumentNullException(nameof(enrichers));
            }

            var batch = ReadBatch(reader, filter, BatchSize, invalidDataHandling);

            while (batch.Length > 0)
            {
                StringContent content;
                using (var builder = new StringWriter())
                {
                    foreach (var evt in batch)
                    {
                        foreach (var enricher in enrichers)
                        {
                            enricher.Enrich(evt, null);
                        }
                        Formatter.Format(evt, builder);
                    }

                    content = new StringContent(builder.ToString(), Encoding.UTF8, ApiConstants.ClefMediatType);
                }

                var result = await connection.Client.HttpClient.PostAsync(ApiConstants.IngestionEndpoint, content);

                if (result.IsSuccessStatusCode)
                {
                    batch = ReadBatch(reader, filter, BatchSize, invalidDataHandling);
                    continue;
                }

                var resultJson = await result.Content.ReadAsStringAsync();

                if (!string.IsNullOrWhiteSpace(resultJson))
                {
                    try
                    {
                        var error = JsonConvert.DeserializeObject <dynamic>(resultJson);

                        Log.Error("Failed with status code {StatusCode}: {ErrorMessage}",
                                  result.StatusCode,
                                  (string)error.ErrorMessage);
                    }
                    catch
                    {
                        // ignored
                    }
                }

                Log.Error("Failed with status code {StatusCode} ({ReasonPhrase})", result.StatusCode, result.ReasonPhrase);
                return(1);
            }

            return(0);
        }