예제 #1
0
 public static LogTrackerContext Init(LogTrackerContextStorageTypeEnum type)
 {
     return(Init(
                type,
                Guid.NewGuid().ToString(),
                DateTime.UtcNow));
 }
예제 #2
0
 /// <summary>
 /// 建立 LogTrackerContext. 會產生一組新的 Request-ID 與 Request-Start-UTCTime, 用來識別與追蹤這一串任務的相關日誌。
 /// 建立好的 LogTrackerContext 關鍵資訊會自動儲存在指定的 Storage, 不需要明確的傳遞下去。只要執行時還維持在同樣的
 /// 執行環境 (context),呼叫 LogTrackerContext.Current 即可取回先前 Create 的 Context 內容。
 /// </summary>
 /// <param name="prefix"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static LogTrackerContext Create(string prefix, LogTrackerContextStorageTypeEnum type)
 {
     return(Init(
                type,
                string.Format("{0}-{1:N}", prefix, Guid.NewGuid()).ToUpper(),
                DateTime.UtcNow));
 }
예제 #3
0
        /// <summary>
        /// 從既有的 context 關鍵資訊 (request-id, request-start-utctime) 來初始化 LogTrackerContext。
        /// 只在串接上一關傳遞過來的 context 時使用,不會產生新的 Request-ID 跟 Request-Start-UTCTime。
        /// 若有需要產生新的 context, 請呼叫 Create( )
        /// </summary>
        /// <param name="type"></param>
        /// <param name="requestId"></param>
        /// <param name="requestStartTimeUTC"></param>
        /// <returns></returns>
        public static LogTrackerContext Init(LogTrackerContextStorageTypeEnum type, string requestId, DateTime requestStartTimeUTC)
        {
            if (String.IsNullOrEmpty(requestId) || String.IsNullOrWhiteSpace(requestId))
            {
                _logger.Error("LogTrackerContext Init Exception: RequestId MUST NOT be null or empty or white space only.");
#if DEBUG
                throw new ArgumentOutOfRangeException("RequestId MUST NOT be null or empty or white space only.");
#endif
                return(null);
            }
            if (requestStartTimeUTC.Kind != DateTimeKind.Utc)
            {
                _logger.Error("LogTrackerContext Init Exception: RequestId MUST NOT be null or empty or white space only.");
#if DEBUG
                throw new ArgumentOutOfRangeException("requestStartTimeUTC MUST be UTC time.");
#endif
                return(null);
            }

            switch (type)
            {
            case LogTrackerContextStorageTypeEnum.ASPNET_HTTPCONTEXT:

                HttpContext.Current.Request.Headers[_KEY_REQUEST_ID]            = requestId;
                HttpContext.Current.Request.Headers[_KEY_REQUEST_START_UTCTIME] = requestStartTimeUTC.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");

                return(Current);

            case LogTrackerContextStorageTypeEnum.THREAD_DATASLOT:
                _thread_static_is_set                = true;
                _thread_static_request_id            = requestId;
                _thread_static_request_start_utctime = requestStartTimeUTC;

                return(Current);

            case LogTrackerContextStorageTypeEnum.NONE:

                return(new LogTrackerContext()
                {
                    StorageType = LogTrackerContextStorageTypeEnum.NONE,
                    RequestId = requestId,
                    RequestStartTimeUTC = requestStartTimeUTC
                });

            case LogTrackerContextStorageTypeEnum.OWIN_CONTEXT:
                throw new NotSupportedException();
            }

            throw new NotSupportedException();
        }
예제 #4
0
        /// <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));
        }
예제 #5
0
        /// <summary>
        /// 清儲存在指定 storage type 的 log context 資訊
        /// </summary>
        /// <param name="type"></param>
        public static void Clean(LogTrackerContextStorageTypeEnum type)
        {
            switch (type)
            {
            case LogTrackerContextStorageTypeEnum.ASPNET_HTTPCONTEXT:

                HttpContext.Current.Request.Headers.Remove(_KEY_REQUEST_ID);
                HttpContext.Current.Request.Headers.Remove(_KEY_REQUEST_START_UTCTIME);
                break;

            case LogTrackerContextStorageTypeEnum.THREAD_DATASLOT:
                _thread_static_is_set                = false;
                _thread_static_request_id            = null;
                _thread_static_request_start_utctime = DateTime.MinValue;
                break;

            case LogTrackerContextStorageTypeEnum.OWIN_CONTEXT:
            case LogTrackerContextStorageTypeEnum.NONE:
                throw new NotSupportedException();
            }
        }