如何在App.config中配置AWS.Logger.SeriLog



我正试图将此AWS.Logger.SeriLog配置示例从json转换为xml,并努力寻找示例和正确的命名约定转换。

"Serilog": {
"Using": [ "AWS.Logger.SeriLog" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"System": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting": "Warning"
}
},
"Enrich": [ "WithMachineName", "FromLogContext", "WithExceptionDetails", "WithAssemblyInformationalVersion" ],
"Region": "us-east-1",
"LogGroup": "MyLogGroup",
"LibraryLogFileName": "Logs/serilog-aws-errors.txt",
"WriteTo": [
{
"Name": "AWSSeriLog",
"Args": {
"textFormatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
]

}

这是我目前在xml

中写的内容
<appSettings>
<add key="serilog:minimum-level" value="Information" />
<add key="serilog:minimum-level:override:Microsoft" value="Warning" />
<add key="serilog:region" value="us-east-1" />
<add key="serilog:log-group" value="MyLogGroup" />
<add key="serilog:library-log-file-name" value="Logs/serilog-aws-errors.txt" />
<add key="serilog:using:AWSSeriLog" value="AWS.Logger.SeriLog" />
<add key="serilog:write-to:AWSSeriLog.textFormatter" value="Serilog.Formatting.Json.JsonFormatter, Serilog" />

<add key="serilog:enrich:WithMachineName" />
<add key="serilog:enrich:FromLogContext" />
<add key="serilog:enrich:WithExceptionDetails" />
<add key="serilog:enrich:WithAssemblyInformationalVersion" />
</appSettings>

这个键在读取配置时导致异常:

<add key="serilog:write-to:AWSSeriLog.textFormatter" value="Serilog.Formatting.Json.JsonFormatter, Serilog" />

我不知道如何将下面的配置添加到应用设置中。Json文件,但你在这里:

//...
using Amazon.Runtime;
using AWS.Logger;
using AWS.Logger.SeriLog;
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Context;
using Serilog.Formatting.Json;
// ...
// create a config object from the appsettings.json file
private static IConfiguration Configuration { get; set; } = new 
ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
// ...
// you can get these configs from your appsettings.json
var awsS3sKey = Configuration["aws:access.key"];
var awsS3Secret = Configuration["aws:secret.key"];
var logGroupName = Configuration["aws:log.group.name"];
var region = Configuration["aws:log.region"];
// create your AWS credential
var credentials = new BasicAWSCredentials(awsS3sKey, awsS3Secret);
// create a logger for AWS Cloudwatch
var awsConfiguration = new AWSLoggerConfig
{
Region = region,
LogGroup = logGroupName,
Credentials = credentials
};
// create a serilog config
var config = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.WriteTo.AWSSeriLog(awsConfiguration, null, new JsonFormatter());
// logger is ready
Log.Logger = config.CreateLogger();
// ...

如果你知道如何将这些设置添加到appsettings文件,请让我知道。

顺便说一下,我在下面使用了这些nuget包:
  • AWS.Logger.SeriLog
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.EnvironmentVariables
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.Logging
  • Serilog
  • Serilog.Enrichers.Context
  • Serilog.Extensions.Logging
  • Serilog.Settings.Configuration
  • Serilog.Sinks.Console
  • Serilog.Sinks.File

相关内容

  • 没有找到相关文章

最新更新