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); }
/// <summary> /// 從既有的 context 物件來初始化 LogTrackerContext。 /// 只在串接上一關傳遞過來的 context 時使用,不會產生新的 Request-ID 跟 Request-Start-UTCTime。 /// 若有需要產生新的 context, 請呼叫 Create( ) /// </summary> /// <param name="type"></param> /// <param name="context"></param> /// <returns></returns> public static LogTrackerContext Init(LogTrackerContextStorageTypeEnum type, LogTrackerContext context) { if (context == null) { Trace.WriteLine(String.Format("{0} | parameter: context can not be NULL.", DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffZ"))); #if DEBUG throw new ArgumentNullException("parameter: context can not be NULL."); #endif return(null); } return(Init( type, context.RequestId, context.RequestStartTimeUTC)); }
public LogTrackerHandler(LogTrackerContext context) { this._context = context; }