Exemplo n.º 1
0
        private static void GetRequestData(PioneerLogEcs detail, HttpContext context)
        {
            if (context.Request == null)
            {
                return;
            }

            detail.Labels.ApplicatoinLocation = context.Request.Path;

            var userAgent  = context.Request.Headers["User-Agent"];
            var uaParser   = Parser.GetDefault();
            var clientInfo = uaParser.Parse(userAgent);

            detail.UserAgent.Original    = clientInfo.ToString();
            detail.UserAgent.Name        = clientInfo.UA.Family;
            detail.UserAgent.Version     = $"{clientInfo.UA.Major}.{clientInfo.UA.Minor}.{clientInfo.UA.Patch}";
            detail.UserAgent.Device.Name = clientInfo.Device.ToString();

            string requestBodyStr;

            using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, true))
            {
                requestBodyStr = reader.ReadToEndAsync().Result;
            }
            detail.Http.Request = new PioneerLogHttpRequest
            {
                Referrer = context.Request.Headers["Referer"],
                MimeType = context.Request.ContentType,
                Method   = context.Request.Method,
                Body     = new PioneerLogHttpRequestBody
                {
                    Bytes   = context.Response.ContentLength,
                    Content = requestBodyStr
                }
            };

            string responseBodyStr = null;

            if (context.Response.Body.CanRead)
            {
                using var reader = new StreamReader(context.Response.Body, Encoding.UTF8, true, 1024, true);
                responseBodyStr  = reader.ReadToEndAsync().Result;
            }
            detail.Http.Response = new PioneerLogHttpResponse
            {
                StatusCode = context.Response.StatusCode,
                MimeType   = context.Response.ContentType,
                Body       = new PioneerLogHttpResponseBody
                {
                    Bytes   = context.Response.ContentLength,
                    Content = responseBodyStr
                }
            };
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get as <see cref="PioneerLog"/> object pre-populated with details parsed
        /// from the ASP.NET Core environment.
        /// </summary>
        public static PioneerLogEcs GetTubEcsDetail(string message,
                                                    LevelEnum level,
                                                    HttpContext context = null,
                                                    Dictionary <string, object> additionalInfo = null)
        {
            var detail = new PioneerLogEcs
            {
                Timestamp  = DateTime.UtcNow,
                Message    = message,
                CustomInfo = additionalInfo,
                Labels     = new PioneerLogLabels
                {
                    ApplicationName         = Configuration.ApplicationName,
                    ApplicationLayer        = Configuration.ApplicationLayer,
                    ApplicationLayerVersion = Configuration.ApplicationLayerVersion
                },
                Event = new PioneerLogEvent
                {
                    Dataset = level.ToString()
                },
                Host = new PioneerLogHost
                {
                    Hostname = Dns.GetHostName(),
                    Host     = Environment.MachineName
                },
                Log = new PioneerLogLog
                {
                    File = new PioneerLogLogFile
                    {
                        Path = @"logs\pioneer-logs-" + level.ToString().ToLower() + "-timestamp-.log"
                    }
                },
                Tracing = new PioneerLogTracing
                {
                    Transaction = new PioneerLogTracingTransaction
                    {
                        Id = IsNullOrEmpty(CorrelationId) ? Guid.NewGuid().ToString() : CorrelationId
                    }
                }
            };

            if (context == null)
            {
                return(detail);
            }

            GetUserData(detail, context);
            GetRequestData(detail, context);

            return(detail);
        }
Exemplo n.º 3
0
        private static void GetUserData(PioneerLogEcs detail, HttpContext context)
        {
            if (context == null || context.User == null || context.User.Claims == null || detail == null)
            {
                return;
            }

            string userId   = null;
            string userName = null;

            var user = context.User;

            var i = 1; // i included in dictionary key to ensure uniqueness

            foreach (var claim in user.Claims)
            {
                if (claim.Type == null)
                {
                    continue;
                }

                switch (claim.Type)
                {
                case ClaimTypes.NameIdentifier:
                    userId = claim.Value;
                    break;

                case "name":
                    userName = claim.Value;
                    break;

                default:
                    if (detail.CustomInfo == null)
                    {
                        detail.CustomInfo = new Dictionary <string, object>();
                    }
                    detail.CustomInfo.Add($"UserClaim-{i++}-{claim.Type}", claim.Value);
                    break;
                }
            }

            if (detail.User == null)
            {
                detail.User = new PioneerLogUser();
            }

            detail.User.Id   = userId;
            detail.User.Name = userName;
        }
Exemplo n.º 4
0
        public PioneerLogsPerformanceTracker(PioneerLogEcs details)
        {
            _sw     = Stopwatch.StartNew();
            _logEcs = details;

            var beginTime = DateTime.Now;

            if (_logEcs.CustomInfo == null)
            {
                _logEcs.CustomInfo = new Dictionary <string, object>
                {
                    { "Started", beginTime.ToString(CultureInfo.InvariantCulture) }
                };
            }
            else
            {
                _logEcs.CustomInfo.Add("Started", beginTime.ToString(CultureInfo.InvariantCulture));
            }
        }
Exemplo n.º 5
0
 public static void WriteError(PioneerLogEcs infoToLog)
 {
     ErrorLogger.Write(LogEventLevel.Information, "{@PioneerLog}", JsonSerializer.Serialize(infoToLog));
 }
Exemplo n.º 6
0
 public static void WriteDiagnostic(PioneerLogEcs infoToLog)
 {
     DiagnosticLogger.Write(LogEventLevel.Information, "{@PioneerLog}", JsonSerializer.Serialize(infoToLog));
 }