serilog / serilog-formatting-compact-reader

A reader for Serilog's compact JSON format
Apache License 2.0
32 stars 16 forks source link

Enable Async Read #24

Closed weirdyang closed 3 months ago

weirdyang commented 4 years ago

Hi, I'm trying to use this in a web api, however, the tryRead is a synchronous action and is causing the application to throw an exception.

Do you recommend enabling Server.AllowSynchronousIO = true?

This is my current workaround:

        [HttpPost("api/log-events")]
        public async Task<IActionResult> Post()
        {

            Serilog.Events.LogEvent evt;
            var apiKey = Request.Headers["X-HEIMDALL-KEY"].FirstOrDefault();
            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var log = new LogEventReader(reader);
                string line;
                while (!string.IsNullOrWhiteSpace(line = await reader.ReadLineAsync()))
                {
                    evt = LogEventReader.ReadFromString(line);
                    Log.Logger.Information(evt.RenderMessage());
                    _publisher.Publish<LogEvent>("test", evt);

                }

            };
            return Ok();
        }
nblumhardt commented 4 years ago

Hi! For now I think allowing synchronous IO will be the quickest way to go, but support for async reads in this library would be a great addition. Marked as up-for-grabs in case anyone following along is interested in exploring/implementing this. Thanks!

Blaisor commented 4 months ago

I'd like to create a new PR for this ticket.

nblumhardt commented 3 months ago

@Blaisor please go ahead 👍