public static async Task <int> Invoke(CancellationToken token, string[] urls, string[] metricUrls, bool metrics, string diagnosticPort, bool noAuth, bool tempApiKey, bool noHttpEgress) { try { AuthConfiguration authenticationOptions = HostBuilderHelper.CreateAuthConfiguration(noAuth, tempApiKey); IHost host = HostBuilderHelper.CreateHostBuilder(urls, metricUrls, metrics, diagnosticPort, authenticationOptions) .ConfigureServices(authenticationOptions, noHttpEgress) .Build(); try { await host.StartAsync(token); await host.WaitForShutdownAsync(token); } catch (MonitoringException) { // It is the responsibility of throwers to ensure that the exceptions are logged. return(-1); } catch (OptionsValidationException ex) { host.Services.GetRequiredService <ILoggerFactory>() .CreateLogger(typeof(CollectCommandHandler)) .OptionsValidationFailure(ex); return(-1); } catch (OperationCanceledException) when(token.IsCancellationRequested) { // The host will throw a OperationCanceledException if it cannot shut down the // hosted services gracefully within the shut down timeout period. Handle the // exception and let the tool exit gracefully. return(0); } finally { if (host is IAsyncDisposable asyncDisposable) { await asyncDisposable.DisposeAsync(); } else { host.Dispose(); } } } catch (FormatException ex) { Console.Error.WriteLine(ex.Message); if (ex.InnerException != null) { Console.Error.WriteLine(ex.InnerException.Message); } return(-1); } return(0); }
public static void Write(Stream stream, string[] urls, string[] metricUrls, bool metrics, string diagnosticPort, bool noAuth, bool tempApiKey, ConfigDisplayLevel level) { IHost host = HostBuilderHelper.CreateHostBuilder(urls, metricUrls, metrics, diagnosticPort, noAuth, tempApiKey).Build(); IConfiguration configuration = host.Services.GetRequiredService <IConfiguration>(); using ConfigurationJsonWriter jsonWriter = new ConfigurationJsonWriter(stream); jsonWriter.Write(configuration, full: level == ConfigDisplayLevel.Full, skipNotPresent: false); }
public static void Main(string[] args) { if (args.Length != 1) { throw new InvalidOperationException("Expected single argument for the output path."); } string outputPath = args[0]; // Create directory if it does not exist Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); // Create all of the same services as dotnet-monitor and add // OpenAPI generation in order to have it inspect the ASP.NET Core // registrations and descriptions. IHost host = HostBuilderHelper .CreateHostBuilder( urls: Array.Empty <string>(), metricUrls: Array.Empty <string>(), metrics: true, diagnosticPort: null, noAuth: false, tempApiKey: false) .ConfigureServices(services => { services.AddSwaggerGen(options => { options.DocumentFilter <BadRequestResponseDocumentFilter>(); options.DocumentFilter <UnauthorizedResponseDocumentFilter>(); options.DocumentFilter <TooManyRequestsResponseDocumentFilter>(); options.OperationFilter <BadRequestResponseOperationFilter>(); options.OperationFilter <RemoveFailureContentTypesOperationFilter>(); options.OperationFilter <TooManyRequestsResponseOperationFilter>(); options.OperationFilter <UnauthorizedResponseOperationFilter>(); var documentationFile = $"{typeof(DiagController).Assembly.GetName().Name}.xml"; var documentationPath = Path.Combine(AppContext.BaseDirectory, documentationFile); options.IncludeXmlComments(documentationPath); }); }) .Build(); // Generate the OpenAPI document OpenApiDocument document = host.Services .GetRequiredService <ISwaggerProvider>() .GetSwagger("v1"); // Serialize the document to the file using StringWriter outputWriter = new(CultureInfo.InvariantCulture); document.SerializeAsV3(new OpenApiJsonWriter(outputWriter)); outputWriter.Flush(); // Normalize line endings before writing File.WriteAllText(outputPath, outputWriter.ToString().Replace("\r\n", "\n")); }
public static IHostBuilder CreateHostBuilder(string[] args) => HostBuilderHelper.CreateHostBuilder <Startup>(args);