예제 #1
0
        public void Test1()
        {
            TraceIdentifiersCollection c = new TraceIdentifiersCollection("c", new[] { "a", "b", "c" });

            Assert.Equal(3, c.All.Count());
            Assert.Equal("c", c.Current);
        }
예제 #2
0
        public static IAppWithTraceIdentifiersBuilder PushToSerilogContext(this IAppWithTraceIdentifiersBuilder app, Action <SerilogTraceIdentifiersOptions> settings = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var options = new SerilogTraceIdentifiersOptions();

            settings?.Invoke(options);

            app.UseSerilogLogContext(e =>
            {
                e.EnrichersForContextFactory = context =>
                {
                    TraceIdentifiersCollection feature = context.Features.Get <TraceIdentifiersCollection>();
                    if (options.SingleProperty)
                    {
                        return(new[]
                        {
                            new PropertyEnricher(options.TraceIdentifiersSinglePropertyName, feature, true),
                        });
                    }

                    return(new[]
                    {
                        new PropertyEnricher(options.TraceIdentifiersCurrentPropertyName, feature?.Current, false),
                        new PropertyEnricher(options.TraceIdentifiersAllPropertyName, feature?.All, true)
                    });
                };
            });

            return(app);
        }
예제 #3
0
        public HttpMessageHandler Create(TraceIdentifiersSendOptions options = null)
        {
            HttpContext httpContext = this._httpContextAccessor.HttpContext;

            if (httpContext == null)
            {
                throw new InvalidOperationException($"Unable to get {nameof(HttpContext)} from {nameof(IHttpContextAccessor)}.");
            }

            TraceIdentifiersCollection identifiers = httpContext.Features.Get <TraceIdentifiersCollection>();

            if (identifiers == null)
            {
                throw new InvalidOperationException($"{nameof(HttpContext)} do not have {nameof(TraceIdentifiersCollection)} feature. Ensure that {nameof(TraceIdentifiersMiddleware)} registered before current one.");
            }

            return(new HttpClientWithTraceIdentifiersHandler(identifiers, options ?? TraceIdentifiersSendOptions.Default));
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseTraceIdentifiers(new TraceIdentifiersMiddlewareOptions
            {
                RemoteSharedHeaderName = "Accept-Encoding",
                RemoteSharedSeparator  = ','
            })
            .PushToSerilogContext();

            app.Run(async(context) =>
            {
                await context.Response.WriteAsync("Hello World! ");
                TraceIdentifiersCollection ti = context.Features.Get <TraceIdentifiersCollection>();

                if (ti != null)
                {
                    await context.Response.WriteAsync($"Current: {ti.Current} ");

                    foreach (var item in ti.All)
                    {
                        await context.Response.WriteAsync(item + "|");
                    }

                    if (!context.Request.Headers.ContainsKey(TraceIdentifiersSendOptions.DefaultHeaderName))
                    {
                        var handler = context.RequestServices.GetRequiredService <IHttpMessageHandlerFactory>();
                        using (HttpClient client = new HttpClient(handler.Create()))
                        {
                            var text = await client.GetAsync("http://localhost:65239");
                            await context.Response.WriteAsync(await text.Content.ReadAsStringAsync());
                        }
                    }
                }

                context.RequestServices.GetRequiredService <ILoggerFactory>().CreateLogger("Demo").LogInformation("Hellow World Log. TraceCurrent: {TraceIdentifier} TraceAll: {TraceIdentifiersCollection}");
            });
        }