Ejemplo n.º 1
0
        public async Task Invoke(HttpContext context /* other dependencies */)
        {
            // TextWriter txtWr;
            try
            {
                var webRoot = _env.ContentRootPath;
                fileName = "log_";

                var path = HelperController.GetPath(webRoot, fileName + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".txt", "logs/");

                File.AppendAllText(path, "--------------------------Request--------------------\n");
                File.AppendAllText(path, await FormatRequest(context.Request));



                var originalBodyStream = context.Response.Body;
                using (var responseBody = new MemoryStream())
                {
                    context.Response.Body = responseBody;

                    await next(context);

                    File.AppendAllText(path, "--------------------------Response--------------------\n");
                    System.IO.File.AppendAllText(path, await FormatResponse(context.Response));
                    await responseBody.CopyToAsync(originalBodyStream);
                }
            }
            catch (Exception ex)
            {
                await HandleExceptionAsync(context, ex);
            }
        }
Ejemplo n.º 2
0
        private static Task HandleExceptionAsync(HttpContext context, Exception ex)
        {
            var webRoot = _env.ContentRootPath;

            fileName = "error_";
            var        path  = HelperController.GetPath(webRoot, fileName + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".txt", "logs/");
            TextWriter txtWr = new StreamWriter(path, true);

            txtWr.WriteLine(JsonConvert.SerializeObject(ex).ToString());
            txtWr.Flush();
            txtWr.Close();
            return(context.Response.WriteAsync(ex.Message));
        }