Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log correlation by request Id on Web applications #1015

Closed
tugberkugurlu opened this issue Oct 21, 2015 · 6 comments

Comments

Projects
None yet
3 participants
@tugberkugurlu
Copy link
Member

commented Oct 21, 2015

I am trying to see how I can correlate the logs based on a request id. It seems like this LoggerMessage.DefineScope is the proper way of doing it but I am not sure how it would play with for the rest of the application where I am not injecting an ILogger but injecting an ILoggerFactory.

Any thoughts or samples on this?

@tugberkugurlu

This comment has been minimized.

Copy link
Member Author

commented Oct 21, 2015

Looking at the beta8 and the below is my poor attempt:

        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(minLevel: LogLevel.Verbose);

            app.Use(async (ctx, next) => 
            {
                var logger = loggerFactory.CreateLogger("temp");
                using(logger.BeginScope(Guid.NewGuid().ToString()))
                {
                    await next();   
                }
            });

            app.UseMvc();
        }

Which got me nowhere.

@Tratcher

This comment has been minimized.

Copy link
Member

commented Oct 21, 2015

Hosting handles this now. See https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs#L97-L110
Note there are a few outstanding PRs to clean it up a little more.

@tugberkugurlu

This comment has been minimized.

Copy link
Member Author

commented Oct 26, 2015

@Tratcher this is very good. One question: how can I get a hold of RequestId so that I can send it through response header? I can see this: https://github.com/aspnet/Mvc/blob/9342cb0ab78add8c527edcbc4c0988edc96f01e3/test/WebSites/RequestServicesWebSite/RequestIdMiddleware.cs but I didn't quite understand how RequestIdService gets the RequestId.

@Tratcher

This comment has been minimized.

Copy link
Member

commented Oct 26, 2015

RequestIdService is just something they made up for their test app. You can just get it from HttpContext.TraceIdentifier https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.Http.Abstractions/HttpContext.cs#L77

@tugberkugurlu

This comment has been minimized.

Copy link
Member Author

commented Oct 28, 2015

@Tratcher thanks! That's very useful, I tried to capture all these info here: http://www.tugberkugurlu.com/archive/asp-net-5-and-log-correlation-by-request-id

@rynowak

This comment has been minimized.

Copy link
Member

commented Oct 28, 2015

That's a really good writeup! Thanks for sharing.

jkotalik pushed a commit that referenced this issue Nov 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.