Exemplo n.º 1
0
        public async Task LogMessageAsync(MessageSeverity severity, string message)
        {
            if (this.IsEnabled(severity))
            {
                await _writer.WriteLineAsync($"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")} {severity.ToString().ToUpper()} {message}");

                // I'm intentioanlly not awaiting this
                _writer.FlushAsync();
            }
        }
Exemplo n.º 2
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task <System.Exception> AsJSON(
            this IDbConnection cnn
            , System.IO.TextWriter output
            , string sql
            , RenderType_t format
            , object param = null
            , IDbTransaction transaction = null
            , int?commandTimeout         = null
            , CommandType?commandType    = null)
        {
            try
            {
                using (System.Data.Common.DbDataReader dr = cnn.ExecuteDbReader(sql, param, transaction, commandTimeout, commandType))
                {
                    using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                               new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                    {
                        if (format.HasFlag(RenderType_t.Indented))
                        {
                            jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                        }


                        // https://stackoverflow.com/questions/15305203/what-to-do-with-errors-when-streaming-the-body-of-an-http-request
                        // https://tools.ietf.org/html/rfc2616#section-14.40
                        // https://stackoverflow.com/questions/50299300/how-to-write-malformed-http-response-to-guarantee-something-akin-to-http-500
                        // https://stackoverflow.com/questions/50298999/write-http-trailer-headers-manually
                        // How do I write a useful trail header to the response that can be displayed well by the browser?
                        // You don’t. Mainstream Web browsers do not care about trailers.
                        // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                        // context.Response.ContentType = "application/json";


                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("tables");

                        await jsonWriter.WriteStartArrayAsync();

                        do
                        {
                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("columns");

                                if (format.HasFlag(RenderType_t.Columns_Associative))
                                {
                                    await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                }
                                else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                {
                                    await WriteComplexArray(jsonWriter, dr, format);
                                }
                                else // (format.HasFlag(RenderType_t.Array))
                                {
                                    await WriteArray(jsonWriter, dr);
                                }
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))


                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WritePropertyNameAsync("rows");
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))

                            await jsonWriter.WriteStartArrayAsync();

                            string[] columns = null;
                            if (format.HasFlag(RenderType_t.DataTable))
                            {
                                columns = new string[dr.FieldCount];
                                for (int i = 0; i < dr.FieldCount; i++)
                                {
                                    columns[i] = dr.GetName(i);
                                } // Next i
                            }     // End if (format.HasFlag(RenderType_t.DataTable))

                            while (await dr.ReadAsync())
                            {
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteStartArrayAsync();
                                }

                                for (int i = 0; i <= dr.FieldCount - 1; i++)
                                {
                                    object obj = await dr.GetFieldValueAsync <object>(i);

                                    if (obj == System.DBNull.Value)
                                    {
                                        obj = null;
                                    }

                                    if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync(columns[i]);
                                    }

                                    await jsonWriter.WriteValueAsync(obj);
                                } // Next i

                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteEndArrayAsync();
                                }
                            } // Whend

                            await jsonWriter.WriteEndArrayAsync();

                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteEndObjectAsync();
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))
                        } while (await dr.NextResultAsync());

                        await jsonWriter.WriteEndArrayAsync();

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.FlushAsync();

                        await output.FlushAsync();
                    } // jsonWriter
                }     // End Using dr
            }         // End Try
            catch (System.Exception ex)
            {
                return(ex);
            } // End Catch

            return(null);
        } // End Task AsJSON