在Worker Service应用程序的Application Insights中配置日志级别



有一个NET5控制台应用程序正在从警告及以上版本进行日志记录。这是后面的文档,但它不适用于记录信息类型。它确实会记录警告。如何将日志级别更改为信息?

class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection();
var startup = new Startup();
startup.ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
var executor = serviceProvider.GetService<IExecutor>();
await executor.ExecuteTestsAsync();
}
}
public class Startup
{
public IConfiguration Configuration { get; }
public Startup()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration);
services.AddLogging();
services.AddApplicationInsightsTelemetryWorkerService();
services.AddSingleton<IExecutor, Executor>();
}
}
public Executor(ILogger<Executor> logger)
{
logger.LogInformation("Hello");//Not logged in AppInsights
...

appsettings.json

{
"ApplicationInsights":
{
"InstrumentationKey": "putinstrumentationkeyhere"
},
"Logging":
{
"LogLevel":
{
"Default": "Information"
}
}
}

也尝试过这个:

services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));

您需要单独指定应用程序洞察的日志级别:

{
"ApplicationInsights":
{
"InstrumentationKey": "putinstrumentationkeyhere"
},
"Logging":
{
"LogLevel":
{
"Default": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}

(来源(

相关内容

  • 没有找到相关文章

最新更新