Question:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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. Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass4_0. Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0. Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0. Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() |
My code is as follows:
In Startup.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "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:
1 2 3 4 |
AWS_ACCESS_KEY_ID=xxxxxxxxxx AWS_DEFAULT_REGION=eu-west-1 AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxx |
Answer:
Just in case anyone has the same problem, this appsettings.json solved it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "Serilog": { "Using": [ "AWS.Logger.SeriLog" ] "Region": "eu-west-1", "LogGroup": "MyLogGroup", "LogLevel": { "Default": "Information", "System": "Information", "Microsoft": "Information" } }, "AllowedHosts": "*" } |