Beispiel #1
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var enrichers = new List <ILogEventEnricher>();

            if (_currentTenant?.Id != null)
            {
                enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.TenantId, _currentTenant.Id));
            }

            if (_currentUser?.Id != null)
            {
                enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.UserId, _currentUser.Id));
            }

            if (_currentClient?.Id != null)
            {
                enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.ClientId, _currentClient.Id));
            }

            var correlationId = _correlationIdProvider.Get();

            if (!string.IsNullOrEmpty(correlationId))
            {
                enrichers.Add(new PropertyEnricher(_options.EnricherPropertyNames.CorrelationId, correlationId));
            }

            using (LogContext.Push(enrichers.ToArray()))
            {
                await next(context).ConfigureAwait(false);
            }
        }
 public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
 {
     logEvent.AddOrUpdateProperty(
         new LogEventProperty(
             "CorrelationId",
             new ScalarValue("CorrId:" + _correlationIdProvider.Get())
             )
         );
 }
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var correlationId = _correlationIdProvider.Get();

            try {
                await next(context);
            } finally {
                CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
            }
        }
Beispiel #4
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var correlationId = _correlationIdProvider.Get();

            try
            {
                await next(context).ConfigureAwait(false);
            }
            finally
            {
                SetCorrelationId(context, _options, correlationId);
            }
        }
Beispiel #5
0
        /// <summary>
        /// 执行中间件拦截逻辑
        /// </summary>
        /// <param name="context">Http上下文</param>
        public async Task InvokeAsync(HttpContext context)
        {
            var correlationId = _correlationIdProvider.Get();

            TraceIdContext.Current ??= new TraceIdContext(correlationId);
            CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
            try
            {
                await _next(context);
            }
            finally
            {
                CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
            }
        }
        /// <summary>
        /// 执行中间件拦截逻辑
        /// </summary>
        /// <param name="context">Http上下文</param>
        public async Task InvokeAsync(HttpContext context)
        {
            var correlationId = _correlationIdProvider.Get();

            TraceIdContext.Current ??= new TraceIdContext(correlationId);
            try
            {
                // TODO: 由于可能存在某个中间件设置了Response导致当前中间件无法设置
                CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
                await _next(context);
            }
            finally
            {
                CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
            }
        }
Beispiel #7
0
        public async Task Invoke(
            HttpContext httpContext,
            IOptions <CorrelationIdOptions> options,
            ICorrelationIdProvider correlationIdProvider)
        {
            var correlationId = correlationIdProvider.Get();
            var optionsValue  = options.Value;

            try
            {
                await _next(httpContext);
            }
            finally
            {
                CheckAndSetCorrelationIdOnResponse(httpContext, optionsValue, correlationId);
            }
        }
Beispiel #8
0
 public ActionResult CorrelationId()
 {
     return(Content(_correlationIdProvider.Get()));
 }