Jer*_*oen 5 c# .net-core asp.net-core .net-core-logging .net-core-configuration
我无法LogTrace(...)在我的应用程序中获得basice 输出.这是一个复制品:
.UseApplicationInsights()以便repro更清晰用以下代码替换代码ValuesController.cs:
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace WebApplication1.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly ILogger<ValuesController> logger;
public ValuesController(ILogger<ValuesController> logger)
{
this.logger = logger;
}
[HttpGet]
public IEnumerable<string> Get()
{
logger.LogError("ERROR!");
logger.LogWarning("WARN!");
logger.LogInformation("INFO!");
logger.LogTrace("TRACE!");
return new string[] { "value1", "value2" };
}
}
}
Run Code Online (Sandbox Code Playgroud)改变appsettings.Development.json这样的:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)运行并查看Debug输出
这导致:
我也试过调整appsettings.json文件中的值,但这也没有效果.
奇怪的是,将文件中的值更改为"Error"也不执行任何操作.
我需要做些什么才能使我的注入ILogger<ValuesController>尊重记录设置,包括Trace级别?
以下是使用上述repro自动生成的一些相关代码:
Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
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, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
Run Code Online (Sandbox Code Playgroud)
Program.cs
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
Run Code Online (Sandbox Code Playgroud)
appsettings.json 默认:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sve*_*vek 12
突破2.0的变化
正如Tseng在下面评论的那样,这个答案将在2.0之后变得过时,你可以在这里找到更多关于这个公告:https://github.com/aspnet/Announcements/issues/238
根据您的Configure()方法,我发现了一个问题:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(); // ? you're not passing the LogLevel!
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
这就是为什么您对appsettings.json文件中的配置集的更改都不起作用的原因.
.AddDebug()不传递任何参数的默认行为是
添加为LogLevel.Information或更高版本启用的调试记录器.
如果要将其显式设置为使用特定的最小LogLevel,则可以将其直接传递给AddDebug(ILoggerFactory, LogLevel)方法.
loggerFactory.AddDebug(LogLevel.Trace);
Run Code Online (Sandbox Code Playgroud)
更多信息可以在这里找到.
LogLevel foo = this.Configuration.GetSection("Logging:LogLevel")
.GetValue<LogLevel>("Default");
loggerFactory.AddDebug(foo);
Run Code Online (Sandbox Code Playgroud)
(故意遗漏.很显然,它提供了这两种方法之间的紧密关系.)我赞成其中一个极端,而不是半途而废
看上去 ConfigurationBinder
var obj = new MyObject();
ConfigurationBinder.Bind(_configuration.GetSection("Logging:LogLevel"), obj);
Run Code Online (Sandbox Code Playgroud)
它将映射到像这样的对象
public class MyObject
{
public LogLevel Default { get; set; }
public LogLevel System { get; set; }
public LogLevel Microsoft { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以你可以通过:
loggerFactory.AddDebug(obj.Default);
Run Code Online (Sandbox Code Playgroud)
请注意,配置的分隔符使用:.
示例:"Logging:LogLevel"将去:
"Logging": {
"IncludeScopes": false,
"LogLevel": { ????? Here
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,以下是有效值LogLevel:
public enum LogLevel
{
Trace = 0,
Debug = 1,
Information = 2,
Warning = 3,
Error = 4,
Critical = 5,
None = 6,
}
Run Code Online (Sandbox Code Playgroud)
来源:https:
//docs.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.logging.loglevel#Microsoft_Extensions_Logging_LogLevel
这对我有用.在ConfigureServices(IServiceCollection services)方法中添加:
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
Run Code Online (Sandbox Code Playgroud)
我试过:
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
Run Code Online (Sandbox Code Playgroud)
问题出在哪里...
这些都没有帮助我。就我而言,我的文件夹中有 appsettings.Development.json 和 exe。该文件已将“默认”设置为“信息”。这就是为什么我看不到跟踪日志的原因。该文件隐藏在 Visual Studio 的解决方案资源管理器中。我必须展开 appsettings.json 才能看到该文件。
更改信息 => 跟踪后,我可以看到严重性为跟踪的日志。
| 归档时间: |
|
| 查看次数: |
19584 次 |
| 最近记录: |