我创建了一个简单的Azure函数,其目的是使用Serililog登录到Azure Blob存储。
当对Serilog接收器使用内联配置时,效果很好,创建接收器,并且Serilog高兴地写入Blob存储。
这有效:
Log.Logger = new LoggerConfiguration()
.WriteTo.AzureBlobStorage(
"STORAGEACCOUNT CONNECTION STRING", // <-- inline config
LogEventLevel.Information,
null,
"{yyyy}/{MM}/{dd}/MyLogFile.txt")
.CreateLogger();
问题是我想通过appsettings.json
进行全部配置,但是当我尝试这样做时(都在模拟器和云中本地运行),它无法绑定接收器设置,并且导致其无法登录到Blog存储。
如果我进行调试,则可以看到配置值已按预期方式加载到configuration
对象中,但它们 not 并未应用于日志配置。
这不起作用:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
appsettings.json
在以下片段中:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [{
"Name": "AzureBlobStorage",
"Args": {
"connectionString": " --- REPLACE WITH STORAGEACCOUNT BLOB CONNECTION STRING --- ",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact",
"storageFileName": "{yyyy}/{MM}/{dd}/MyLogFile.txt",
"retainedFileCountLimit": 31
}
}
],
"Properties": {
"Application": "int-test-logging",
"Environment": "int"
}
}
}
我不确定自己做错了什么,但会有所帮助。以下github存储库包含实现以上两种方法的代码,并重现了此行为。 https://github.com/oneiltomlinson/AzureFunctionsLogging
答案 0 :(得分:6)
只需添加
"Using": [ "Serilog.Sinks.AzureBlobStorage" ] //under Serilog Config
它将起作用!
工作配置
{
"Serilog": {
"Using": [
"Serilog.Sinks.AzureBlobStorage"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "AzureBlobStorage",
"Args": {
"connectionString": " --- REPLACE WITH STORAGEACCOUNT BLOB CONNECTION STRING --- ",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact",
"storageFileName": "{yyyy}/{MM}/{dd}/MyLogFile.txt",
"retainedFileCountLimit": 31
}
}
],
"Properties": {
"Application": "int-test-logging",
"Environment": "int"
}
}
}
答案 1 :(得分:1)
我遇到了同样的问题,即使已使用的接收器全部需要“使用”,也没有任何变化。这是我的配置文件(与Serilog部分差不多):
"Serilog": {
"Using": [
"Serilog.Sinks.RollingFile",
"Serilog.Sinks.AzureTableStorage"
],
"MinimumLevel": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
},
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u3}] [TID:{ThreadId}] - {Message:lj}{NewLine}{Exception}"
},
{
"Name": "Debug",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u3}] [TID:{ThreadId}] - {Message:lj}{NewLine}{Exception}"
},
{
"Name": "AzureTableStorage",
"Args": {
"storageTableName": "HeroFunctionsLogs",
"connectionString": "DefaultEndpointsProtocol=https;AccountName=herobugari;AccountKey=NYJcdoCg9mQxqbQaTUdIxOHYQGdcKAwMsuWwzDub29UweHR1c+wd6Sh3IDQNB+eCx3DAe/ccobxu67sJvqQB5g==",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u3}] [TID:{ThreadId}] - {Message:lj}{NewLine}{Exception}",
"restrictedToMinimumLevel": "Information",
"writeInBatches": true,
"batchPostingLimit": 100
}
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": "logs\\Bulgari.Hero.Functions.log",
"retainedFileCountLimit": 10,
"buffered": true,
"flushToDiskInterval": 5,
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u3}] [TID:{ThreadId}] - {Message:lj}{NewLine}{Exception}"
}
}
],
"Filter": [
{
"Name": "ByExcludingOnly",
"Args": {
"expression": "SourceContext like '%Microsoft.EntityFrameworkCore%'"
}
}
]
}
}
}
]
}
,然后读取并尝试使用它的Startup.cs代码:
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables().Build();
// Registering Serilog provider
logger = new LoggerConfiguration()
//.WriteTo.Console()
//.WriteTo.File("logs\\herofunctions.log", rollingInterval: RollingInterval.Day)
.ReadFrom.Configuration(config)
.CreateLogger();
builder.Services.AddLogging(lb => lb.AddSerilog(logger));
记录器对象没有在问题中注册的“接收器”。
答案 2 :(得分:1)
通过在local.settings.json中添加条目,我已经解决了完全相同的问题。
"Serilog:Using:0": "Serilog.Sinks.Console",
"Serilog:MinimumLevel": "Information",
"Serilog:Override:Microsoft": "Warning",
"Serilog:Override:System": "Warning",
"Serilog:WriteTo:Console:Name": "Console"
然后在StartUp.cs中:
var provider = builder.Services.BuildServiceProvider();
var config = provider.GetRequiredService<IConfiguration>();
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(config).CreateLogger();
然后该功能:
public class TestFunction
{
private readonly ILogger _logger;
public TestFunction()
{
_logger = Log.Logger;
}
[FunctionName("TestFunction")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req)
{
_logger.Information("Hello");
return new OkResult();
}
}
}
我可以看到已注册的配置条目并记录了条目!