我有一个应用程序设置为使用 Application Insights,并且我正在尝试手动记录一些自定义信息。这是我的 Controller :
public class MyController : Controller
{
private ILogger<MyController> Logger { get; set; }
public MyController(ILogger<MyController> logger)
{
Logger = logger;
}
public IActionResult Index()
{
Logger.LogCritical("Test critical");
Logger.LogError("Test error");
Logger.LogWarning("Test warning");
Logger.LogInformation("Test information");
Logger.LogDebug("Test debug");
Logger.LogTrace("Test trace");
...
}
我的 Startup.cs 中有这个:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
...
}
这在我的 Program.cs 中:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();
在我的 appsettings.json 中:
"Logging": {
"IncludeScopes": false,
"ApplicationInsights": {
"LogLevel": {
"Default": "Warning"
}
},
"LogLevel": {
"Default": "Warning"
}
}
当我在 Azure 门户中查看 Application Insights 时,唯一记录的内容是:
因此,由于某种原因,它会跳过一些内容,只记录“严重”、“警告”和“错误”。我主要想使用LogInformation
。
我的日志设置或启动文件中是否需要更改某些内容?
最佳答案
如果您想要收集所有遥测数据,则不应在 appsettings.json
中指定 Warning
logLevel。 Warning
日志级别只会收集 Warning
/Error
/Critical
数据,而放弃 Trace
/调试
/信息
数据。
请在 appsettings.json
中将应用程序洞察的日志级别指定为 Trace
,如下所示:
"Logging": {
"IncludeScopes": false,
"ApplicationInsights": {
"LogLevel": {
"Default": "Trace"
}
},
"LogLevel": {
"Default": "Warning"
}
}
关于c# - 并非所有日志级别都记录在 Application Insights 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60290524/