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

Aspire .Net usage #771

Open
haldiggs opened this issue Oct 31, 2024 · 4 comments
Open

Aspire .Net usage #771

haldiggs opened this issue Oct 31, 2024 · 4 comments
Labels
feature-request A request for new functionality

Comments

@haldiggs
Copy link

Is your feature request related to a problem?

It could be I just don't know how to effectively use the package.

I am trying to set this up for an aspire xunit test. These use the IDistributedApplicationTestingBuilder.

    readonly IDistributedApplicationTestingBuilder appHost = DistributedApplicationTestingBuilder.CreateAsync<Projects.AspirePoC_AppHost>().Result;

    //in the [Fact] code
    readonly ITestOutputHelper logger = testOutputHelper;

    appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
    {
        clientBuilder.AddStandardResilienceHandler();
    }).AddLogging((builder) => {
        builder.ClearProviders().AddXUnit(logger);
    });

I have a logger message in the /weather service call and one in the unit test itself. I see the later message but never the one inside the server call.

Describe the solution you'd like

Describe alternatives you've considered

None

Additional context

Screenshot 2024-10-31 140306

@haldiggs haldiggs added the feature-request A request for new functionality label Oct 31, 2024
@martincostello
Copy link
Owner

Looking at your code snippet, you've configured logging for the client calls, but you haven't changed the logging settings for the servers themselves?

I haven't used DistributedApplicationTestingBuilder myself, but I assume you need to configure logging via appHost.Services too? Try something like:

appHost.Services.AddLogging((builder) => builder.AddXUnit(logger))

@haldiggs
Copy link
Author

haldiggs commented Nov 1, 2024

sorry I may not understand... the snippet I sent was from the [Fact], sorry I'm not pro'ed on github markup...

in the unit test the appHost is setup simply:
readonly IDistributedApplicationTestingBuilder appHost = DistributedApplicationTestingBuilder.CreateAsync<Projects.AspirePoC_AppHost>().Result;

inside the Fact I am adding to the appHost:
appHost.Services.ConfigureHttpClientDefaults(clientBuilder => { clientBuilder.AddStandardResilienceHandler(); }).AddLogging((builder) => { builder.ClearProviders().AddXUnit(logger); });

inside the web application I still see the messages that I have coded to log, as you normally would in the structured logs. As you know, when using Xunit you don't get to see them in the unit tests like you did using Microsoft Test. I thought adding this code would let the ITestOutputHelper be used as the logger in the webapp so I'd see the logs in the unit test.

but... if you have not yet tried Aspire I can understand that is might be difficult to grasp with the crummy snipets :). Sorry... I guess it just won't do what I had hoped at this time.

@martincostello
Copy link
Owner

Your code snippet makes it looks like you're adding xunit to the client logging - but you're not adding it to the logging of your services. My snippet is a suggestion of how to do that.

@haldiggs
Copy link
Author

haldiggs commented Nov 1, 2024

I think I see what you mean. I thought the chained call would still work. I made this change.

        appHost.Services.AddLogging((builder) => builder.AddXUnit(logger));
        appHost.Services.ConfigureHttpClientDefaults(clientBuilder =>
        {
            clientBuilder.AddStandardResilienceHandler();
        });

I still only see the logger from inside the [Fact]. For some reason this does not replace the logger created by the IDistributedApplicationTestingBuilder. bummer. Meaning logging from inside the API method is never seen.

I do appreciate you getting back to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A request for new functionality
Projects
None yet
Development

No branches or pull requests

2 participants