web-dev-qa-db-ja.com

ASP NET Core 2.0 appsettings.Development.jsonがログ設定で機能しない

VS2017 15.3.0 Preview 4と.NET Core 2.0 Preview 2をインストールし、デフォルトのWeb MVCアプリケーションを作成しました。新しいロギング機能の動作を確認したいのですが、デバッグ出力ウィンドウを表示すると、VSでappsettings.Development.jsonに定義されているロギング値を使用できません。

私の理解では、appsettings.Development.jsonファイルはappsettings.jsonよりも優先されますが、後者のファイルの値のみがデバッグウィンドウに影響を及ぼします。これは正解?その場合、追加のセットアップが必要ですか?

ここに値と結果があります...

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "None"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

appsettings.Development.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

デバッグ時の空の出力(Application InsightsのTelemetryレコードのみが表示されているため、まだ取り除く方法を考えていません)

Empty Output

しかし、appsettings.jsonでログレベルを変更すると、期待どおりに出力が表示されます...

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

デバッグ時の新しい出力(Microsoft.AspNetCore.Hosting.Internal.WebHost:Informationが含まれていることに注意してください)

enter image description here

私のStartup.csファイルは、以下のように新しいプロジェクトで作成されたデフォルトのASP.NET Core 2.0テンプレートです。また、appsettings.jsonファイルとappsettings.Development.jsonファイルの両方も、新しいプロジェクトテンプレートによって自動的に作成されました。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

これは、ASP.NET Core 2.0 MVCテンプレートのデフォルトでもあるProgram.csです。

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

[〜#〜]ソリューション[〜#〜]

デフォルトでは、dev "appsettings.Development.json"設定ファイルはメインの "appsettings.json"設定ファイルの後に読み込まれるため、dev設定が優先されます。ただし、デフォルトのappsettings.Development.jsonファイルには、奇妙に思われるログレベル設定のデバッグノードが含まれていません。これが実際の開発設定です。

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "None",
      "System": "None",
      "Microsoft": "None"
    },
    "Debug": {
      "LogLevel": {
        "Default": "Information",
        "System": "None",
        "Microsoft": "Information"
      }
    }
  }
}
8
OjM

appsettings.jsonのロガーごとの設定は、appsettings.Development.jsonのグローバルカテゴリのデフォルトよりも優先されます。

appsettings.jsonにはDebugロガーのログレベルNoneがあるため、Visual Studioの出力ウィンドウからのログは表示されません。

appsettings.Development.jsonから。[SOMECODE] _からのものを上書きするには、appsettings.jsonで.NET Core 2.0のロガーごとの形式(プレビュー2)に従う必要があります。

{
  "Logging": {
    "<Logger>": {
      "LogLevel": {
        "Default": "<LogLevel>"
      }
    }
  }
}

明確にするために、デバッグロガーにTraceログレベルが必要な場合、json構成では次のようになります。

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Trace"
  }
}
3
Dealdiane