示例#1
0
 private LogMetadata BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
 {
     logMetadata.ResponseStatusCode  = (int)response.StatusCode;
     logMetadata.ResponseTimestamp   = DateTime.UtcNow.ToLongTimeString();
     logMetadata.ResponseContentType = response.Content.Headers.ContentType.MediaType;
     return(logMetadata);
 }
示例#2
0
 private static LogMetadata BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
 {
     logMetadata.ResponseStatusCode  = response.StatusCode;
     logMetadata.ResponseTimestamp   = DateTime.Now;
     logMetadata.ResponseContentType = response.Content.Headers.ContentType.MediaType;
     return(logMetadata);
 }
        private async Task <bool> SendToLog(LogMetadata logMetadata)
        {
            _dbContext.LogMetadata.Add(logMetadata);
            await _dbContext.SaveChangesAsync();

            return(true);
        }
示例#4
0
        private async Task <bool> SendToLog(LogMetadata logMetadata)
        {
            //preferably use NLogger or something but it's much simpler this way to integrate w/o existing db, and task says to save logs to DB, not to file
            _unitOfWork.Logs.Add(logMetadata);
            await _unitOfWork.SaveAsync();

            return(true);
        }
    private async Task <LogMetadata> BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
    {
        logMetadata.ResponseStatusCode  = response.StatusCode;
        logMetadata.ResponseTimestamp   = DateTime.Now;
        logMetadata.ResponseContentType = response.Content == null ? string.Empty : response.Content.Headers.ContentType.MediaType;
        logMetadata.Response            = await response.Content.ReadAsStringAsync();

        return(logMetadata);
    }
示例#6
0
        private LogMetadata BuildRequestMetadata(HttpRequestMessage request)
        {
            LogMetadata log = new LogMetadata
            {
                RequestMethod    = request.Method.Method,
                RequestTimestamp = DateTime.UtcNow.ToLongTimeString(),
                RequestUri       = request.RequestUri.ToString()
            };

            return(log);
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            LogMetadata logMetadata = BuildRequestMetadata(request);

            Logger.TraceFormat("Initiated request: \"{0}\" {1}", logMetadata.Method, logMetadata.RequestUri);
            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

            logMetadata = BuildResponseMetadata(logMetadata, response);
            Logger.TraceFormat("Completed request: {0} with status {1} {2}", logMetadata.ResponseUri, logMetadata.Scheme, (int)logMetadata.StatusCode);
            return(response);
        }
示例#8
0
        private static LogMetadata BuildRequestMetadata(HttpRequestMessage request)
        {
            var log = new LogMetadata
            {
                RequestMethod    = request.Method.Method,
                RequestTimestamp = DateTime.Now,
                RequestUri       = request.RequestUri.ToString()
            };

            return(log);
        }
 private async Task <bool> SendToLog(LogMetadata logMetadata)
 {
     try
     {
         //write your code
     }
     catch
     {
         return(false);
     }
     return(true);
 }
    private async Task <LogMetadata> BuildRequestMetadata(HttpRequestMessage request)
    {
        LogMetadata log = new LogMetadata
        {
            RequestMethod    = request.Method.Method,
            RequestTimestamp = DateTime.Now,
            RequestUri       = request.RequestUri.ToString(),
            RequestContent   = await request.Content.ReadAsStringAsync(),
        };

        return(log);
    }
        private LogMetadata BuildRequestMetadata(HttpRequestMessage request)
        {
            LogMetadata log = new LogMetadata
            {
                ClientIP         = GetClientIp(request),
                RequestMethod    = request.Method.Method,
                RequestTimestamp = DateTime.Now,
                RequestUri       = request.RequestUri.ToString()
            };

            return(log);
        }
示例#12
0
        private LogMetadata BuildRequestMetadata(HttpRequestMessage request, HttpActionContext actionContext)
        {
            LogMetadata log = new LogMetadata()
            {
                Message          = "Executing",
                ActionMethod     = actionContext.ActionDescriptor.ActionName,
                RequestUri       = request.RequestUri.AbsolutePath,
                RequestMethod    = request.Method.Method,
                RequestTimestamp = DateTime.Now
            };

            return(log);
        }
示例#13
0
        private LogMetadata BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
        {
            logMetadata.StatusCode        = response.StatusCode;
            logMetadata.ResponseTimestamp = DateTime.Now;
            logMetadata.ResponseUri       = response.RequestMessage.RequestUri.ToString();
            logMetadata.Scheme            = response.RequestMessage.RequestUri.Scheme.ToUpper();
            MediaTypeHeaderValue contentType = response.Content.Headers.ContentType;

            if (contentType != null)
            {
                logMetadata.ResponseContentType = contentType.MediaType;
            }
            return(logMetadata);
        }
示例#14
0
        private LogMetadata BuildRequestMetadata(HttpRequestMessage request)
        {
            IEnumerable <String> referrerFullUri;
            var data = new LogMetadata
            {
                RequestMethod = request.Method.Method,
                RequestUri    = request.RequestUri.ToString(),
                RequestBody   = MaskAttributes(request)
            };

            if (request.Headers.TryGetValues("ReferrerFullUri", out referrerFullUri))
            {
                data.ReferrerFullUri = referrerFullUri.FirstOrDefault();
            }
            return(data);
        }
示例#15
0
        private async Task <bool> SendToLog(HttpRequestMessage request, HttpResponseMessage response,
                                            DateTime requestTimestamp, DateTime responseTimestamp, CancellationToken cancellationToken)
        {
            var logEntry = new LogMetadata
            {
                RequestMethod        = request.Method.Method,
                RequestTimestamp     = requestTimestamp,
                RequestUri           = request.RequestUri.ToString(),
                ResponseStatusCode   = response.StatusCode,
                ResponseTimestamp    = responseTimestamp,
                ResponseContentType  = response.Content?.Headers.ContentType.MediaType,
                DurationMilliseconds = (responseTimestamp - requestTimestamp).TotalMilliseconds
            };
            await Task.Run(() => logger.Debug(logEntry.ToString()), cancellationToken);

            return(true);
        }
示例#16
0
        private LogMetadata BuildRequestMetadata(HttpRequestMessage request)
        {
            LogMetadata logMetadata = new LogMetadata
            {
                Method           = request.Method.Method,
                RequestTimestamp = DateTime.Now,
                RequestUri       = request.RequestUri.ToString(),
            };
            MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;

            if (contentType != null)
            {
                logMetadata.RequestContentType = contentType.MediaType;
            }

            return(logMetadata);
        }
示例#17
0
        private LogMetadata BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
        {
            if (logMetadata.HasException)
            {
                logMetadata.Message           = "Exception found!!!";
                logMetadata.ResponseTimestamp = DateTime.Now;
            }
            else
            {
                logMetadata.Message             = "Executed";
                logMetadata.ResponseContentType = response.Content.Headers.ContentType.ToString();
                logMetadata.ResponseStatusCode  = response.StatusCode;
                logMetadata.ResponseTimestamp   = DateTime.Now;
            }


            return(logMetadata);
        }
        public Task <bool> SendLogData(LogMetadata logMetadata)
        {
            bool   isDataPosted = false;
            string command      = string.Empty;

            using (sqlConnection = new SqlConnection(connection))
            {
                command                = "LogMetaDataProc";
                sqlCommand             = new SqlCommand(command, sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.Add("@LogDetails", SqlDbType.Structured).Value = CreateDataTable(logMetadata);
                sqlConnection.Open();
                if (sqlCommand.ExecuteNonQuery() > 0)
                {
                    isDataPosted = true;
                }
            }
            return(Task.FromResult(isDataPosted));
        }
示例#19
0
        public static async Task LogData(DataContext context, HttpRequest req, DateTime reqTimestamp, dynamic res)
        {
            DateTime responseTimestamp;
            TimeSpan requestProcessingTime;

            responseTimestamp     = DateTime.UtcNow;
            requestProcessingTime = responseTimestamp.Subtract(reqTimestamp);

            LogMetadata log = new LogMetadata(
                req.ContentType,
                req.Path,
                req.Method,
                reqTimestamp,
                res.StatusCode,
                responseTimestamp,
                $"{req.Path} was requested at {reqTimestamp} with {req.Method} method, request was processed in {requestProcessingTime.TotalMilliseconds} milliseconds, response status code {res.StatusCode}",
                requestProcessingTime.TotalMilliseconds
                );

            context.Logs.Add(log);
            await context.SaveChangesAsync();
        }
        public async Task InvokeAsync(HttpContext httpContext)
        {
            string severity = "INFO";
            string message  = "My Demo Message";

            log = new LogMetadata();
            var responseBodyStream = new MemoryStream();
            var bodyStream         = httpContext.Response.Body;

            try
            {
                ReadRequestBody(httpContext);

                httpContext.Response.Body = responseBodyStream;

                await _next(httpContext);
            }
            catch (Exception ex)
            {
                severity = "Exception";
                _logger.LogError($"Something went wrong (from Custom Middleware): {ex}");
                await HandleExceptionAsync(httpContext, ex);
            }
            finally
            {
                responseBodyStream.Seek(0, SeekOrigin.Begin);
                log.ResponseBody        = new StreamReader(responseBodyStream).ReadToEnd();
                log.ResponseTimestamp   = DateTime.Now;
                log.ResponseContentType = httpContext.Response.ContentType;
                log.ResponseStatusCode  = (HttpStatusCode)httpContext.Response.StatusCode;

                responseBodyStream.Seek(0, SeekOrigin.Begin);
                await responseBodyStream.CopyToAsync(bodyStream);

                await SplunkManager.LogDataToSplunk(severity, message, log);
            }
        }
示例#21
0
 private async Task <bool> SendToLog(LogMetadata logMetadata)
 {
     // TODO: Write code here to store the logMetadata instance to a pre-configured log store...
     return(true);
 }
示例#22
0
        private async Task SendToLog(bool onExecuting, LogMetadata logMetadata)
        {
            Task task = null;

            if (onExecuting)
            {
                task = Task.Factory.StartNew(() =>
                {
                    logger.Debug("{0} {1} method" + Environment.NewLine + "Uri: {2}, Verb: {3}, Date: {4}",
                                 logMetadata.Message, logMetadata.ActionMethod, logMetadata.RequestUri, logMetadata.RequestMethod, logMetadata.RequestTimestamp);
                }
                                             );

                await task;
            }
            else
            {
                if (logMetadata.HasException)
                {
                    task = Task.Factory.StartNew(() =>
                    {
                        /*logger.Error(string.Format(Environment.NewLine +
                         *  "Exception: {0}" + Environment.NewLine +
                         *  "Content Type: {1}" + Environment.NewLine +
                         *  "Verb: {2}" + Environment.NewLine +
                         *  "Status Code: {3}" + Environment.NewLine +
                         *  "Date: {4}", logMetadata.Exception, logMetadata.RequestContentType,
                         *  logMetadata.RequestMethod, logMetadata.ResponseStatusCode, logMetadata.ResponseTimestamp));*/

                        /*logger.Error(string.Format(Environment.NewLine + "{0}" +
                         *  Environment.NewLine + "Exception: {1}" +
                         *  Environment.NewLine + "Verb: {2}" +
                         *  Environment.NewLine + "Status Code: {3}" +
                         *  Environment.NewLine + "Date: {4}" + logMetadata.Message, logMetadata.Exception, logMetadata.RequestMethod, logMetadata.ResponseStatusCode));*/

                        logger.Error($@": {Environment.NewLine} {logMetadata.Message}
                                    Exception: {logMetadata.Exception} {Environment.NewLine}
                                    Verb: {logMetadata.RequestMethod} {Environment.NewLine}");
                    }
                                                 );

                    await task;
                }
                else
                {
                    task = Task.Factory.StartNew(() =>
                    {
                        logger.Debug(string.Format(Environment.NewLine +
                                                   "Message: {0} {1}",
                                                   "Content Type: {2}" + Environment.NewLine +
                                                   "Verb: {3}" + Environment.NewLine +
                                                   "Status Code: {4}" + Environment.NewLine +
                                                   "Date: {5}", logMetadata.Message, logMetadata.ActionMethod,
                                                   logMetadata.RequestContentType, logMetadata.RequestMethod,
                                                   logMetadata.ResponseStatusCode, logMetadata.ResponseTimestamp));
                    }
                                                 );

                    await task;
                }
            }
        }