Ejemplo n.º 1
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
                {
                }
            }
        }
        public void Subscripe()
        {
            var channel = _rabbitmqChannelManagement.GetChannel(Name);
            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += async(sender, args) =>
            {
                var message   = Encoding.UTF8.GetString(args.Body.Span);
                var traceInfo = message.ToObj <TraceInfo>();
                if (traceInfo != null)
                {
                    await _transPortService.Send(traceInfo);
                }
                channel.BasicAck(args.DeliveryTag, false);
            };

            channel.BasicConsume(Name, false, consumer);
        }