ASP.NET Core日志记录太冗长 - c#

我在ASP.NET Core 2.1 WebApi应用程序中的配置日志记录方面遇到麻烦。我成功实现了将消息记录到Azure并在日志流中检查消息,但是这些日志太冗长。我不希望类别Microsoft.AspNetCore中的消息被登录到信息级别。这是我在appsettings.json文件中的日志记录部分:

"Logging": {
  "LogLevel": {
    "Default": "Debug",
    "Microsoft.AspNetCore.Hosting.Internal.WebHost": "Debug",
    "GameHub": "Information" 
   }
}

还有我的Program类:

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("appsettings.json", optional:true, reloadOnChange:true); })
            .ConfigureLogging((ctx, logging) =>
                {
                    logging.AddConfiguration(ctx.Configuration.GetSection("Logging"));
                });
}

它仍然在信息级别而不是调试级别记录来自类别Microsoft.AspNetCore的消息。

我究竟做错了什么?

参考方案

如Sellotape所说,.NET Core中设置的日志级别是最低级别。

设置Debug时,它将记录Critical, Error, Warning, Information, Debug级别。它不会记录Trace(最高详细程度)。

如果您不想使用Information,则将其设置为Warning,则只记录Critical, Error, Warning

但是,如果要Critical, Error, Warning, Debug没有信息,则不能直接使用appsettings.json进行操作。

public static class Program
{
    public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) => { ... })
            .ConfigureLogging((webhostContext, builder) => {
                builder.AddConfiguration(webhostContext.Configuration.GetSection("Logging"))
                .AddFilter<ConsoleLoggerProvider>(logLevel => logLevel!=LogLevel.Information)
                .AddConsole()
                .AddDebug();
            })
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights();
}

// by strong typedProvider
.AddFilter<ConsoleLoggerProvider>(logLevel => logLevel!=LogLevel.Information)
// or by name
.AddFilter("Console", logLevel => logLevel != LogLevel.Information)
// or generic/global
.AddFilter(logLevel => logLevel != LogLevel.Information)

如ASP.NET Core Documentation所示,添加带有三个谓词(Func<string, string, LogLevel, bool>Func<string, LogLevel, bool>Func<LogLevel, bool>)之一的日志记录过滤器:

过滤功能

对于没有通过配置或代码分配规则的所有提供者和类别,将调用过滤器功能。函数中的代码可以访问提供程序类型,类别和日志级别。例如:

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureLogging(logBuilder =>
    {
        logBuilder.AddFilter((provider, category, logLevel) =>
        {
            if (provider == "Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider" && 
                category == "TodoApiSample.Controllers.TodoController")
            {
                return false;
            }
            return true;
        });
    })
    .Build();

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

ASP.NET-如何更改JSON序列化的方式? - javascript

我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …

ASP.NET MVC中应为DataTable返回哪种数据? - javascript

我想为DataTable中的每个页面创建动态加载信息。我正在尝试遵循以下示例:https://www.datatables.net/manual/server-sidehttps://www.datatables.net/manual/data来自示例的代码:$('#example').DataTable( { serverSide: t…

Asp.Net:在服务器端还原DropDownList的客户端SelectedItem - c#

因此,我的页面上有一个dropDownList,其中包含数百个项目。用户可以通过在文本框中键入一些文本来过滤此DDL。然后对DDL进行相应的过滤(所有不包含输入文本的项目都将通过JavaScript删除)。然后,用户选择他的项目并按下按钮。通常,这将导致错误,因为DDL已更改并且ASP验证了PostBack数据。但是,使用EnableEventValidat…

asp.net mvc中的对象数组数据始终为null - javascript

我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…