I would appreciate some guidance in setting up a Serilog sink for AWS CloudWatch with .NET Core. I'm using appsettings.json for configuration but I am unable to put the settings in the logger. When trying to write log information to CloudWatch this error appears:
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:
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:
{
"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_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:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"Serilog": {
"Using": [ "AWS.Logger.SeriLog" ]
"Region": "eu-west-1",
"LogGroup": "MyLogGroup",
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"AllowedHosts": "*"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With