Ejemplo n.º 1
0
        protected virtual void HttpResponseHandle(HttpResponseMessage response)
        {
            var activity1 = System.Diagnostics.Activity.Current;

            activity1?.AddTag("4", "4");
            activity1?.AddBaggage("4", "4");
            if (Options.RequestRule.Invoke(ServiceProvider, HttpContextAccessor?.HttpContext?.Request))
            {
                string serviceName = null;
                if (response.RequestMessage.Headers.TryGetValues(HttpConstant.TRACEMICROSERVICE, out IEnumerable <string> serviceNames))
                {
                    serviceName = serviceNames?.FirstOrDefault();
                }

                if (HttpContextAccessor.HttpContext != null && HttpContextAccessor.HttpContext.Items.ContainsKey(DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)))
                {
                    var parentTraceInfoBuilder = HttpContextAccessor.HttpContext.Items[DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)] as TraceInfoBuilder;
                    if (parentTraceInfoBuilder != null)
                    {
                        var parentTraceInfo  = parentTraceInfoBuilder.Build();
                        var traceInfoBuilder = TraceInfoBuilder.CreateBuilder().BuildFromTraceInfo(parentTraceInfo).ParentId(parentTraceInfo.Id).HttpRequestMessage(response.RequestMessage)
                                               .HttpResponseMessage(response).ElapsedTime(HttpContextAccessor.HttpContext.ElapsedTime()).TargetServerName(serviceName).Log(LogLevel.Trace);

                        Logger.LogInformation(traceInfoBuilder);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
            TraceInfo traceInfo = null;

            if (eventId == DiagnosticConstant.EVENT_ID)
            {
                if (state is string)
                {
                    var str = state as string;
                    traceInfo = str.ToObj <TraceInfo>();
                }
                else if (state is TraceInfo)
                {
                    traceInfo = state as TraceInfo;
                }
                else if (state is TraceInfoBuilder)
                {
                    var builder = state as TraceInfoBuilder;
                    traceInfo = builder?.Build();
                }
            }
            else
            {
                if (_httpContextAccessor.HttpContext != null && _httpContextAccessor.HttpContext.Items.ContainsKey(DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)))
                {
                    var parentTraceInfoBuilder = _httpContextAccessor.HttpContext.Items[DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)] as TraceInfoBuilder;
                    if (parentTraceInfoBuilder != null)
                    {
                        var parentTraceInfo  = parentTraceInfoBuilder.Build();
                        var traceInfoBuilder = TraceInfoBuilder.CreateBuilder().BuildTraceInfo(Guid.NewGuid().ToString()).ParentId(parentTraceInfo.Id)
                                               .TrackId(parentTraceInfo.TrackId).ParentTrackId(parentTraceInfo.ParentTrackId)
                                               .Log(logLevel, _categoryName, exception).Description(state?.ToString());
                        traceInfo = traceInfoBuilder.Build();
                    }
                }
            }
            //通过异步发送TraceInfo
            if (traceInfo != null)
            {
                try
                {
                    var buffer = Encoding.UTF8.GetBytes(traceInfo.ToJson());
                    var model  = _rabbitmqChannelManagement.GetChannel(TraceLogRabbitmqConsumer.NAME);
                    model.BasicPublish("", TraceLogRabbitmqConsumer.NAME, null, buffer);
                }
                catch
                {
                }
            }
        }
        private TraceInfoBuilder createErrorLogBuilder(HttpContext httpContext)
        {
            var builder = httpContext.Items[DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)];

            if (builder != null && builder is TraceInfoBuilder traceInfoBuilder)
            {
                var elapsedTime         = httpContext.ElapsedTime();
                var traceInfo           = traceInfoBuilder.Build();
                var traceInfoBuilderNew = TraceInfoBuilder.CreateBuilder().BuildFromTraceInfo(traceInfo).ParentId(traceInfo.Id)
                                          .HttpContext(httpContext).ElapsedTime(elapsedTime);
                return(traceInfoBuilderNew);
            }
            return(null);
        }
Ejemplo n.º 4
0
 protected virtual void HttpExceptionHandle(HttpRequestMessage request, Exception exception)
 {
     if (Options.RequestRule.Invoke(ServiceProvider, HttpContextAccessor?.HttpContext?.Request))
     {
         if (HttpContextAccessor.HttpContext != null && HttpContextAccessor.HttpContext.Items.ContainsKey(DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)))
         {
             var parentTraceInfoBuilder = HttpContextAccessor.HttpContext.Items[DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName)] as TraceInfoBuilder;
             if (parentTraceInfoBuilder != null)
             {
                 var parentTraceInfo  = parentTraceInfoBuilder.Build();
                 var traceInfoBuilder = TraceInfoBuilder.CreateBuilder().BuildFromTraceInfo(parentTraceInfo).ParentId(parentTraceInfo.Id).HttpRequestMessage(request)
                                        .ElapsedTime(HttpContextAccessor.HttpContext.ElapsedTime()).Exception(exception).Log(LogLevel.Trace);
                 Logger.LogInformation(traceInfoBuilder);
             }
         }
     }
 }
        protected virtual void BeginRequestHandle(HttpContext httpContext)
        {
            if (Options.RequestRule.Invoke(ServiceProvider, httpContext.Request))
            {
                var request = httpContext.Request;
                //上一个服务传过来 是父级的跟踪Id
                var parentTrackId = request.Headers[HttpConstant.TRACK_ID].FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(parentTrackId))
                {
                    request.Headers.Add(HttpConstant.PARENT_TRACK_ID, parentTrackId);
                }
                var trackId = Guid.NewGuid().ToString();
                request.Headers.Add(HttpConstant.TRACK_ID, trackId);

                //当前服务追踪的Id
                var chainId = Guid.NewGuid().ToString();
                request.Headers.Add(HttpConstant.CHAIN_ID, chainId);
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                httpContext.Items.Add(DiagnosticConstant.GetItemKey(HttpConstant.TRACK_TIME), stopwatch);
                var traceInfoBuilder = TraceInfoBuilder.CreateBuilder().BuildTraceInfo(chainId).TrackId(trackId, parentTrackId).HttpContext(httpContext);
                httpContext.Items.Add(DiagnosticConstant.GetItemKey(typeof(TraceInfoBuilder).FullName), traceInfoBuilder);
            }
        }