asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-core-mvc – ASP NET Core 2.0 appsettings.Development.json无法使用日志记录配置大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_874_0@
我已经安装了VS2017 15.3.0 Preview 4和.NET Core 2.0 Preview 2,并创建了一个默认的Web MVC应用程序.我有兴趣了解新的日志记录功能如何工作但我无法让VS在查看Debug输出窗口时使用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遥测记录,我还没有找到如何摆脱它)

但是,如果我更改appsetTings.json中的日志级别,那么我会看到输出符合预期…

appsetTings.json

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

调试时的新输出(请注意,Microsoft.AspNetCore.HosTing.Internal.WebHost:现在包含信息)

我的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?}");
        });
    }
}

这是我的Program.cs,它也是ASP.NET Core 2.0 MVC模板的默认设置.

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","Microsoft": "Information"
      }
    }
  }
}

解决方法

appsetTings.json中的每个记录器设置优先于appsetTings.Development.json的全局类别默认值.

您没有看到Visual studio输出窗口中的任何日志,因为您的appsetTings.json的调试记录器的日志级别为None.

您需要在appsetTings.Development.json中遵循.NET Core 2.0的每个记录器格式(预览2),这应该是下面的格式,以覆盖appsetTings.json中的格式.

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

为了清楚起见,如果你想要一个Debug logger的Trace日志级别,这就是它在你的json配置中应该是这样的

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

大佬总结

以上是大佬教程为你收集整理的asp.net-core-mvc – ASP NET Core 2.0 appsettings.Development.json无法使用日志记录配置全部内容,希望文章能够帮你解决asp.net-core-mvc – ASP NET Core 2.0 appsettings.Development.json无法使用日志记录配置所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。