stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
558 stars 106 forks source link

Examples don't seem to work for .net 6 mvc web app #112

Open sarchibald-mdsol opened 1 year ago

sarchibald-mdsol commented 1 year ago

I've created a new .net6 mvc web app using vs2022 & the standard project template. When I add via nuget the CorrelationId package, and modify program.cs and then run, my first page load shows:

The 'X-Correlation-ID' request header is required, but was not found.

Here's my code ...

           builder.Services.AddDefaultCorrelationId(options =>
            {
                options.CorrelationIdGenerator = () => Guid.NewGuid().ToString();
                options.AddToLoggingScope = true;
                options.EnforceHeader = true;
                options.IgnoreRequestHeader = false;
                options.IncludeInResponse = true;
                options.RequestHeader = "My-Custom-Correlation-Id";
                options.ResponseHeader = "X-Correlation-Id";
                options.UpdateTraceIdentifier = false;
            });
            var app = builder.Build();
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCorrelationId();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
stevejgordon commented 1 year ago

@sarchibald-mdsol Are you including a header My-Custom-Correlation-Id in your request? With EnforceHeader=true, this is the expected behaviour if incoming requests do not include the header. I cannot reproduce an issue when the expected header is present.