serilog-formatting-compact-reader icon indicating copy to clipboard operation
serilog-formatting-compact-reader copied to clipboard

Enable Async Read

Open weirdyang opened this issue 4 years ago • 1 comments

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();
        }

weirdyang avatar Aug 02 '20 12:08 weirdyang

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!

nblumhardt avatar Aug 02 '20 22:08 nblumhardt

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

Blaisor avatar Jul 12 '24 15:07 Blaisor

@Blaisor please go ahead 👍

nblumhardt avatar Jul 12 '24 21:07 nblumhardt