Пример #1
0
        private void Logging(RequestLogRow log)
        {
            var date        = DateTime.Now;
            var file        = this.directoryPath + date.ToString("yyyy-MM-dd");
            var row         = new StringBuilder();
            var formatFirst = " | {0}: {1}";
            var formatBody  = "\n\t{0}: {1}";

            row.AppendLine();
            row.Append(date.ToString("HH:mm:ss"));
            row.AppendFormat(formatFirst, "RequestId", log.RequestId);
            row.AppendFormat(formatFirst, "Status", log.Status);
            row.AppendFormat(formatBody, "Query", log.Query);
            row.AppendFormat(formatBody, "Body", log.Body);
            row.AppendFormat(formatBody, "User", log.User);
            row.AppendFormat(formatBody, "Ip", log.Ip);
            row.AppendFormat(formatBody, "Elapsed", log.Elapsed);
            row.AppendLine();

            lock (locker)
            {
                System.IO.File.AppendAllText(file, row.ToString());
            }
        }
Пример #2
0
        public async Task InvokeAsync(HttpContext context)
        {
            var stopwatch = Stopwatch.StartNew();
            var request   = new RequestLogRow
            {
                RequestId = context.TraceIdentifier,
                Query     = $"{context.Request.Method} {context.Request.Path}",
                Body      = await GetRequestBody(context.Request),
                Ip        = context.Connection.RemoteIpAddress.ToString()
            };

            await this._next.Invoke(context);

            request.Status = context.Response?.StatusCode;
            request.User   = context.User.Identity.Name;

            stopwatch.Stop();
            request.Elapsed = stopwatch.ElapsedMilliseconds;

            var task = Task.Run(() =>
            {
                this.Logging(request);
            });
        }