我正在尝试使用 Serilog 作为我在 Razor Pages 应用程序中的
ILogger
实现。我发现,当我将 ILogger
注入页面模型时,它总是变成一个 SilentLogger
,它什么都不记录。奇怪的是,如果页面模型有一个注入的服务,并且该服务也注入了ILogger
,那么日志记录在那里可以正常工作——只有在页面模型级别才会出现这个问题。
这是我的 Serilog 设置:
builder.Host.UseSerilog((context, configuration) =>
{
configuration
.WriteTo.Console();
});
这是我的页面模型:
public class TestPageModel : PageModel
{
private readonly ITestService _testService; // logging from within this service works
private readonly ILogger<TestModel> _logger;
public TestModel(ITestService testService, ILogger<TestModel> logger)
{
_testService = testService;
_logger = logger;
}
public void OnGet()
{
_logger.LogError(new Exception("test exception"), "Test exception"); // this doesn't work
}
}