Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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
like image 263
JPG Avatar asked Nov 15 '18 13:11

JPG


1 Answers

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": "*"
}
like image 55
JPG Avatar answered Sep 16 '22 14:09

JPG