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 { } } }