gpt4 book ai didi

c# - 如何配置 Serilog 接收器以记录到 CloudWatch

转载 作者:行者123 更新时间:2023-12-03 23:14:44 27 4
gpt4 key购买 nike

我希望能在使用 .NET Core 为 AWS CloudWatch 设置 Serilog 接收器方面得到一些指导。
我正在使用 appsettings.json 进行配置,但我无法将设置放入记录器中。尝试将日志信息写入 CloudWatch 时,会出现此错误:

An error occurred while starting the application.
AmazonClientException: No RegionEndpoint or ServiceURL configured

Amazon.Runtime.ClientConfig.Validate() in ClientConfig.cs, line 446

AmazonClientException: No RegionEndpoint or ServiceURL configured

Amazon.Runtime.ClientConfig.Validate() in ClientConfig.cs
Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config) in AmazonServiceClient.cs
AWS.Logger.Core.AWSLoggerCore..ctor(AWSLoggerConfig config, string logType) in AWSLoggerCore.cs
AWS.Logger.SeriLog.AWSSink..ctor(AWSLoggerConfig loggerConfiguration, IFormatProvider iFormatProvider, ITextFormatter textFormatter) in AWSSink.cs
AWS.Logger.SeriLog.AWSLoggerSeriLogExtension.AWSSeriLog(LoggerSinkConfiguration loggerConfiguration, IConfiguration configuration, IFormatProvider iFormatProvider, ITextFormatter textFormatter) in AWSLoggerSeriLogExtension.cs
...
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions+InjectApiVersionRoutePolicy+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass4_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

我的代码如下:

在 Startup.cs 中:
public Startup(IHostingEnvironment env)
{
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration.GetSection("AWS.Logging"))
.WriteTo.AWSSeriLog(Configuration)
.CreateLogger();

app.UseMvc();
}

我的 appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AWS.Logging": {
"Region": "eu-west-1",
"LogGroup": "MyLogGroup",
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"AllowedHosts": "*"
}

我的开发人员机器上的 AWS 凭证是使用环境变量设置的:
AWS_ACCESS_KEY_ID=xxxxxxxxxx
AWS_DEFAULT_REGION=eu-west-1
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxx

最佳答案

以防万一有人遇到同样的问题,这个 appsettings.json 解决了它:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"Serilog": {
"Using": [ "AWS.Logger.SeriLog" ]
"Region": "eu-west-1",
"LogGroup": "MyLogGroup",
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"AllowedHosts": "*"
}

关于c# - 如何配置 Serilog 接收器以记录到 CloudWatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53321096/

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