[英]How to configure a Serilog sink for logging to CloudWatch
I would appreciate some guidance in setting up a Serilog sink for AWS CloudWatch with .NET Core.我将不胜感激为使用 .NET Core 的 AWS CloudWatch 设置 Serilog 接收器的一些指导。 I'm using appsettings.json for configuration but I am unable to put the settings in the logger.
我正在使用 appsettings.json 进行配置,但我无法将设置放入记录器中。 When trying to write log information to CloudWatch this error appears:
尝试将日志信息写入 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()
My code is as follows:我的代码如下:
In Startup.cs:在 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();
}
My appsettings.json:我的 appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AWS.Logging": {
"Region": "eu-west-1",
"LogGroup": "MyLogGroup",
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"AllowedHosts": "*"
}
Credentials for AWS from my developer machine are set by using environment variables:我的开发人员机器上的 AWS 凭据是使用环境变量设置的:
AWS_ACCESS_KEY_ID=xxxxxxxxxx
AWS_DEFAULT_REGION=eu-west-1
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxx
Just in case anyone has the same problem, this appsettings.json solved it:以防万一有人遇到同样的问题,这个 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": "*"
}
I don't think the previous solution is complete, it misses the WriteTo
part, the minimal should look like this:我不认为前面的解决方案是完整的,它遗漏了
WriteTo
部分,最小的应该是这样的:
"Serilog": {
"Using": [
"AWS.Logger.SeriLog"
],
"LogGroup": "MyLogGroup",
"Region": "eu-west-1",
"WriteTo": [
{
"Name": "AWSSeriLog",
"Args": {
"textFormatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
This requires Serilog.Formatting.Compact library to be installed (I recommend using it, much neater log records), you can also use the default:这个需要安装Serilog.Formatting.Compact库(推荐使用,日志记录更整洁),也可以使用默认的:
"textFormatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
This setup is utilising the AWS supplied library AWS.Logger.Serilog , do not forget to add it to your NuGet packages.此设置使用 AWS 提供的库AWS.Logger.Serilog ,不要忘记将其添加到您的 NuGet 包中。
Also, if you are not running the app inside the AWS, the AWS credentials are to be provided.此外,如果您没有在 AWS 中运行该应用程序,则需要提供 AWS 凭证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.