gpt4 book ai didi

azure - 如何在Azure Function中配置Serilog设置?

转载 作者:行者123 更新时间:2023-12-03 02:12:09 25 4
gpt4 key购买 nike

我正在创建我的第一个生产 Azure Function,并尝试将日志信息写入本地文件,然后最终写入 Blob 存储。本地文件更多用于开发故障排除,最终我希望将生产信息存储在 Blob 存储中。我不仅是 Azure Functions 的新手,而且也是 Serilog 的新手。我在所有其他应用程序中使用了 NLog,但无法让它与 Azure Functions 一起使用。

目前我正在尝试让本地日志正常工作。我实际上似乎可以正常工作,但我不明白如何调整一些东西。

我想要改变的第一件事是记录的信息量。它似乎正在记录一大堆系统类型的信息,例如向 blob 存储请求信息。记录的内容太多,以至于我在代码中添加的条目都丢失了。看起来所有系统条目都被标记为信息,这就是它可能显示在我的日志中的原因。但是,我想看看是否可以让它仅记录我在代码中专门调用 logger.Information(“some text”) 时的数据。有没有办法隐藏所有的微软系统信息?

第二件事是如何使 Serilog 配置来 self 的 local.settings.json 文件。下面是我的文件示例,我不确定是否要在 Values: 属性中添加配置信息,还是将其放在该属性之外,放入其自己的属性中?我假设它将位于它自己的属性中,但到目前为止我所有的自定义设置都来自 Values: 属性?

我需要添加 Serilog.Settings.Configuration NuGet 包吗?如果是这样,那么我不明白如何配置 Startup.cs 文件以从本地设置文件获取信息,而不是直接在代码中配置设置。最终,我想将它添加到依赖注入(inject)中,这样我也可以在其他类中使用记录器。

Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IDataManager, DataManager>();

ConfigureServices(builder.Services).BuildServiceProvider(true);
}

private IServiceCollection ConfigureServices(IServiceCollection services)
{
services
.AddLogging(loggingBuilder =>
loggingBuilder.AddSerilog(
new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File(@"D:\logs\AzureFunction\log_.txt", rollingInterval: RollingInterval.Day)
.CreateLogger())
);

return services;
}

Local.settings.json

{
"IsEncrypted": false,
"Values": {
"ProcessLookBackDays": "90",
"SqlConnection": "connection info",
"StorageConnection": "connection info"
"AzureWebJobsStorage": "connection info"
"InputContainer": "test-files",
"InputFolder": "input",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "D:\\logs\\AzureFunction\\log_.log",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3}] {Username} {Message:lj}{NewLine}{Exception}"
}
}
]
}
}

最佳答案

  • 本地设置中设置配置。 Json 不会反射(reflect)在 azure 函数应用程序中。顾名思义,local-setting 仅供本地使用,而使用 azure 时,您需要使用应用程序设置并读取它们。

enter image description here

  • 只需在应用设置中添加新设置,然后您就可以使用以下代码读取设置。
var appsettings = Environment.GetEnvironmentVariable("Name of the setting");
  • 当您想将外部配置与serilog一起使用时,您可以使用Serilog.Settings.Configuration

  • 现在您可以配置日志事件的最低级别,这样所有重要性低于指定最低级别的日志事件都不会被记录。

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
  • 这里我们指定了两件事:.MinimumLevel.Debug()restrictedToMinimumLevel 该属性规定了该特定接收器的最低级别。接收器只是您可以记录日志的地方,它们是使用 Writeto 标签配置的。例如,在上面的代码中,接收器是一个控制台。有other也下沉了。

最低级别为详细调试、信息、警告、错误、致命。

引用:-

Read configuration from app setting作者:阿什什·帕特尔

Use serilog to filter the logs

Serilog setting configuration

关于azure - 如何在Azure Function中配置Serilog设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73045274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com