public SpanContextFactory(ITraceIdGenerator traceIdGenerator) { _traceIdGenerator = traceIdGenerator; }
public async Task Invoke(HttpContext httpContext, ITrafficCometMiddlewaresAccessor trafficCometAccessor, ITrafficLogWriter logWriter, ITrafficLogFactory logFactory, ITraceIdGenerator traceIdGenerator, IClientIdGenerator clientUniqueIdGenerator, IOptionsSnapshot <TrafficCometMiddlewareConfig> config) { if (trafficCometAccessor == null) { throw new ArgumentNullException(nameof(trafficCometAccessor)); } if (logWriter == null) { throw new ArgumentNullException(nameof(logWriter)); } if (logFactory == null) { throw new ArgumentNullException(nameof(logFactory)); } if (traceIdGenerator == null) { throw new ArgumentNullException(nameof(traceIdGenerator)); } if (clientUniqueIdGenerator == null) { throw new ArgumentNullException(nameof(clientUniqueIdGenerator)); } TrafficCometMiddlewaresAccessor internalTrafficCometAccessor = (TrafficCometMiddlewaresAccessor)trafficCometAccessor; internalTrafficCometAccessor.InitContextValues(); internalTrafficCometAccessor.ApplicationId = config.Value.ApplicationId; internalTrafficCometAccessor.IgnoreWholeRequest = config.IgnoreRequest(httpContext.Request.Path); var clientIdReaded = clientUniqueIdGenerator.TryGenerateClientId(out string clientId); var traceIdReaded = traceIdGenerator.TryGenerateTraceId(out string traceId); if (!internalTrafficCometAccessor.IgnoreWholeRequest && clientIdReaded && traceIdReaded) { BeforeExecuteNextMiddleware(ref internalTrafficCometAccessor, clientId, traceId); await Next(httpContext); try { AfterExecutedNextMiddleware(ref internalTrafficCometAccessor); var ignoreRequest = internalTrafficCometAccessor.IgnoreRequest; var ignoreResponse = internalTrafficCometAccessor.IgnoreResponse; var log = logFactory.Create(); if (log != null) { _ = logWriter.SaveLog(log); } } catch (Exception ex) { if (Logger != null && Logger.IsEnabled(LogLevel.Error)) { Logger.LogError(new EventId(92, $"TrafficComet.{nameof(TrafficCometMiddleware)}"), ex, ex.ToString()); } } } else { await Next(httpContext); } }