Пример #1
0
        public LogTrackerHandler()
        {
            this._context = LogTrackerContext.Current;

            //
            if (this._context == null)
            {
                // prefix: HC (HttpClient)
                this._context = LogTrackerContext.Create("TEMP-HC", LogTrackerContextStorageTypeEnum.NONE);
            }
        }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            Logger _log = LogManager.GetLogger("LogTracker");

            try
            {
                //
                // TODO: 後續版本必須移除。LogTrackerContext 不應該 "全自動" 的產生,必須有明確進入點才能建立新的 LogTrackerContext.
                //
                if (LogTrackerContext.Current == null)
                {
                    _log.Info("creating request_id and request_start_time_utc.");
                    {
                        if (string.IsNullOrEmpty(this.Prefix))
                        {
                            LogTrackerContext.Create("TEMP", LogTrackerContextStorageTypeEnum.ASPNET_HTTPCONTEXT);
                        }
                        else
                        {
                            LogTrackerContext.Create(this.Prefix, LogTrackerContextStorageTypeEnum.ASPNET_HTTPCONTEXT);
                        }
                    }
                    _log.Info("request_id and request_start_time_utc created.");
                }

                var    arguments   = GetArguments(actionContext.ActionArguments);
                string requestJson = JsonConvert.SerializeObject(arguments, _jsonSettings);

                _logger.Info(new LogMessage()
                {
                    Message   = $"Before call {Prefix} {actionContext.ControllerContext.Request.RequestUri.AbsolutePath}",
                    ExtraData = new Dictionary <string, object>()
                    {
                        { "RequestId", LogTrackerContext.Current.RequestId },
                        { "RequestUri", actionContext.Request.RequestUri.AbsoluteUri },
                        { "RequestBody", actionContext.Request.Content.ReadAsStringAsync().Result },
                        { "ModelBindingRequest", requestJson },
                    }
                });
            }
            catch (Exception ex)
            {
                _log.Warn(ex);
            }
            base.OnActionExecuting(actionContext);
        }