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

Example of using dotnet watch with Aspire #885

Closed
En3Tho opened this issue Nov 16, 2023 · 4 comments
Closed

Example of using dotnet watch with Aspire #885

En3Tho opened this issue Nov 16, 2023 · 4 comments

Comments

@En3Tho
Copy link

En3Tho commented Nov 16, 2023

This might come in handy for others who are curious how to run projects with dotnet watch enabled. Basically it's a full copy of what AddProject does but with ExecutableResource instead of ProjectResource. I'm not sure this requires a pull request but maybe it is worth being an example in the docs or something like that.

public static  IResourceBuilder<ExecutableResource> AddProjectWithDotnetWatch<TProject>(this IDistributedApplicationBuilder builder, string name) where TProject : IServiceMetadata, new()
    {
        var serviceMetadata = new TProject();
        var project = new ExecutableResource(name, "dotnet", Path.GetDirectoryName(serviceMetadata.ProjectPath)!, ["watch", "--non-interactive"]);
        var executableBuilder = builder.AddResource(project);
        // We only want to turn these on for .NET projects, ConfigureOtlpEnvironment works for any resource type that
        // implements IDistributedApplicationResourceWithEnvironment.
        executableBuilder.WithEnvironment("OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES", "true");
        executableBuilder.WithEnvironment("OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES", "true");
        executableBuilder.WithOtlpExporter();
        executableBuilder.WithEnvironment((context) =>
        {
            if (context.PublisherName == "manifest")
            {
                return;
            }

            // Enable ANSI Control Sequences for colors in Output Redirection
            context.EnvironmentVariables["DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION"] = "true";

            // Enable Simple Console Logger Formatting with a UTC timestamp similar to RFC3339Nano that Docker generates
            context.EnvironmentVariables["LOGGING__CONSOLE__FORMATTERNAME"] = "simple";
            context.EnvironmentVariables["LOGGING__CONSOLE__FORMATTEROPTIONS__TIMESTAMPFORMAT"] =
                $"{TimestampParser.DisplayFormat} ";
        });
        executableBuilder.WithAnnotation(serviceMetadata);
        return executableBuilder;
    }
@DamianEdwards
Copy link
Member

We intend to update dotnet watch to properly support working with Aspire AppHost projects. I've logged dotnet/sdk#36971 to track this feature.

@danmoseley
Copy link
Member

Thanks @En3Tho . I'm going to close this as I don't think there's further action but folks can find your example.

@En3Tho
Copy link
Author

En3Tho commented Nov 17, 2023

@danmoseley Should there be a tracking issue for dotnet watch support in Aspire? Maybe I've missed it? Or that sdk issue is enough?

@danmoseley
Copy link
Member

I think the SDK issue will likely cover it but you'd be welcome to open a linked issue here for the scenario if there isn't one.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants