.NET 8 Windows 服务在本地调试时认为托管环境是生产环境

问题描述 投票:0回答:1

我们有一个作为 Windows 服务运行的 .NET 8 应用程序。

问题:在执行 IHostBuilder.ConfigureAppConfiguration 并检查 IHost.HostingEnvironment 时,它显示为 Production。

看起来 IHostBuilder 默认将环境变量视为 Production ?

项目中没有任何地方将其设置为作为生产运行。

我检查了以下位置的 ASPNETCORE_ENVIRONMENT 值:

  • launchsettings.json - 设置为“开发”
  • 系统/用户环境变量 - ASPNETCORE_ENVIRONMENT 未在那里设置,因此应默认选择“开发”
  • 整个项目 - ASPNETCORE_ENVIRONMENT 没有被设置为 Production。

有谁知道为什么会发生这种情况以及如何解决这个问题?

Program.cs代码:

using LinqToDB.EntityFrameworkCore;
using PosJobs.LT.Server;
using PosJobs.LT.Server.Ioc;
using Sentry.Serilog;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;

const long LogFileSizeLimit = 5 * 1024 * 1024;
var baseDirectory = AppContext.BaseDirectory;

IHost host = Host.CreateDefaultBuilder(args)
    .UseWindowsService(options =>
    {
        options.ServiceName = "PosJobs.LT";
    })
    .ConfigureAppConfiguration((ctx, builder) =>
    {
        var environment = ctx.HostingEnvironment.EnvironmentName;

        builder.SetBasePath(baseDirectory);
        builder.AddJsonFile("appsettings.json", false, true);
        builder.AddJsonFile($"appsettings.{environment}.json", true, true);
        builder.AddEnvironmentVariables();
    })
    .ConfigureServices((ctx, services) =>
    {
        var configuration = ctx.Configuration;

        services.ConfigureHangfire(ctx);
        services.ConfigureOptions(configuration);
        services.ConfigureServices(configuration);
        services.ConfigureDbContexts(configuration);
    })
    .UseSerilog((hostingContext, loggerConfiguration) =>
    {
        var f = new FileInfo(baseDirectory);
        var drive = Path.GetPathRoot(f.FullName);

        var logPath = @$"{drive}\Logs\PosJobs.LT\log_.json";

        loggerConfiguration
            .MinimumLevel.Information()
            .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore.Routing.EndpointMiddleware", LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore.Authorization.DefaultAuthorizationService", LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker",
                LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware", LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Error)
            .MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Error)
            .WriteTo.Console()
            .WriteTo.File(new CompactJsonFormatter(),
                logPath,
                shared: true,
                restrictedToMinimumLevel: LogEventLevel.Information,
                retainedFileCountLimit: 90,
                rollingInterval: RollingInterval.Day,
                rollOnFileSizeLimit: true,
                fileSizeLimitBytes: LogFileSizeLimit);

        if (!hostingContext.HostingEnvironment.IsDevelopment())
        {
            loggerConfiguration.WriteTo.Sentry(options =>
            {
                var optionsToSet = hostingContext.Configuration.GetSection("SentrySerilog").Get<SentrySerilogOptions>();

                options.Dsn = optionsToSet.Dsn;
                options.Environment = optionsToSet.Environment;
                options.StackTraceMode = optionsToSet.StackTraceMode;
                options.MinimumEventLevel = optionsToSet.MinimumEventLevel;
                options.MinimumBreadcrumbLevel = optionsToSet.MinimumBreadcrumbLevel;
                options.InitializeSdk = true;
            });
        }
    })
    .Build();

IConfiguration config = host.Services.GetRequiredService<IConfiguration>();
config.RegisterJobs(host.Services);

LinqToDBForEFTools.Initialize();

await host.RunAsync();

更新#1

经过一些阅读后,显然通用 IHost 构建器检查 DOTNET_ 变量,而不是 ASPNETCORE_。将此变量添加到 launchsettings.json,现在它可以正常工作了。

但我的问题仍然存在:为什么通用 IHost 构建器默认将 DOTNET_ENVIRONMENT 视为生产?

c# .net asp.net-core .net-core
1个回答
0
投票

这是因为如果两个变量均未设置,则默认值为 Production。

生产:如果是 DOTNET_ENVIRONMENT 则默认 ASPNETCORE_ENVIRONMENT 尚未设置。

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0

© www.soinside.com 2019 - 2024. All rights reserved.