I have successfully added some custom logging to my request using Carter in C#.
this is what my startup.cs code looks like :
public void Configure(IApplicationBuilder app,IHostingEnvironment env) {
app.UseCarter(this.RequestId())
app.UseCarter(this.Log())
}
public CarterOptions Log(){
return new CarterOptions(ctx => this.BeforeLog(ctx), ctx => AfterLog(ctx))
}
public CarterOptions RequestId(){
return new CarterOptions(ctx => this.AddRequestId(ctx))
}
private Task<bool> AddRequestId() {
ctx.Request.Header["x-request-id"] = Guid.NewGuid().ToString()
}
The problem is if I comment app.UseCarter(this.RequestId()) then all the requests are successfully logged both going in and out.
But If I uncomment it, only the request which fail are logged.
Can someone clarify if I am doing something wrong here?
The multiple options calls are causing conflicts with creating the middleware.
consider refactoring to a more explicit statement that clarifies what it is you are actually trying to do.
Based on conversation in comments
What you describe is like having a middleware like feature for the middleware's options.
It is doable but it wont be pretty.
I thought about it and came up with the following builder for the
CarterOptions
classAfter some unit tests to confirm that it behaves as expected, it can be used like the following based on your original code