简体   繁体   English

asp.net framework 4.7 配置日志级别,不工作

[英]asp.net framework 4.7 configure logging level, not working

My web job is not adhering to the logging level configuration in my host.json.我的 Web 作业不符合我的 host.json 中的日志记录级别配置。 It is written using .NET framework 4.7.它是使用 .NET 框架 4.7 编写的。 The expected result with the below code is no logging, instead, I get logging a per the below screenshot.下面代码的预期结果是没有日志记录,相反,我根据下面的屏幕截图记录了一个。 Can any provide guidance on how to fix this?任何人都可以提供有关如何解决此问题的指导吗? Do I need to add additional configuration/startup code?我需要添加额外的配置/启动代码吗?

在此处输入图片说明

host.json主机文件

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "Functions.Test": "None",
      "default": "None"
    }
  }
}

C# Web Job Code C# Web 作业代码

public static void Main()

        {

            var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("host.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            var builder = new HostBuilder();

            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddTimers();
                b.AddBuiltInBindings();
            });

            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();
            });

            var host = builder.Build();
            using (host)
            {
                var jobHost = host.Services.GetService(typeof(IJobHost)) as JobHost;
                jobHost.CallAsync("Test").GetAwaiter().GetResult();
            }
        }
    }

    public class Functions
    {
        [NoAutomaticTrigger]
        public static void Test(ILogger logger)
        {
            logger.LogCritical("LogCritical");
            logger.LogDebug("LogDebug");
            logger.LogError("LogError");
            logger.LogInformation("LogInformation");
            logger.LogTrace("LogTrace");
            logger.LogWarning("LogWarning");
            Thread.Sleep(1000 * 10);
        }
    }
}

thanks for sharing your code.感谢您分享您的代码。

It seems to me that in the first statement a config object is being built var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()... , however, later in the code this config variable is not used while host creation and configuration.在我看来,在第一条语句中,正在构建配置对象var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()... ,但是,在代码的后面,在主机创建和配置时不使用此config变量。 As a result, those settings are not applied and default Information logLevel is used.因此,不会应用这些设置,而是使用默认的Information logLevel。

We need to supply this configuration to the HostBuilder , looks like it can be done using HostBuilder 's ConfigureAppConfiguration method.我们需要将此配置提供给HostBuilder ,看起来可以使用HostBuilderConfigureAppConfiguration方法来完成。 Please take a look at this question , it has an example how to use this method.请看一下这个问题,它有一个如何使用这种方法的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET Core中分离应用程序级日志记录和框架级日志记录 - Separating application level logging and framework level logging in ASP.NET Core Serilog 在.Net Framework 4.7 上不起作用 - Serilog Not Working on .Net Framework 4.7 具有完整.NET Framework(.NET 4.7)的ASP.NET Core的简单注入器 - Simple Injector with ASP.NET Core With Full .NET Framework (.NET 4.7) 在.net Framework 4.7(NOT ASP.NET Core)中为MVC5设置有效的生产和消费媒体类型 - Setting valid produce and consume media types in swagger for MVC5 in .net Framework 4.7 (NOT ASP.NET Core) 在 ASP.NET Boilerplate 中为应用程序服务配置审计日志 - Configure audit logging for Application Services in ASP.NET Boilerplate ASP.NET Core 2.2 实体框架日志记录 - ASP.NET Core 2.2 Entity Framework Logging 如何关闭由 ASP.NET 核心框架完成的日志记录 - How to turn off the logging done by the ASP.NET core framework 使用2实体框架ASP.Net API - Working with 2 Entity Framework ASP.Net API ASP.NET实体框架SqlQuery无法正常工作 - ASP.NET Entity Framework SqlQuery not working 如何为ASP.net Core配置Entity Framework 6 - How to configure Entity Framework 6 for ASP.net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM