Reputation: 107
so I added serilog for logging I was using it locally to log on files, but I want use serilog to update the logs on AWS cloud watch or add a log group on the cloud watch but I don't know where to add the secret and key or how to configure serilog to use them, should I add them as environment variables? what is the best practice?
StartUp
app.UseSerilogRequestLogging();
appsettings.Develppment.json
"Serilog": {
"Using": [ "Serilog.Sinks.File", "AWS.Logger.SeriLog" ],
"Region": "us-west-2",
"LogGroup": "AWS:Cloudwatch:LogGroup",
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "logs/logs_{Date}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},
{
"Name": "File",
"Args": {
"path": "logs/Test.txt",
"outputTemplate": "{Timestamp:G} {Message}{NewLine:1}{Exception:1}"
}
},
{
"Name": "File",
"Args": {
"path": "logs/Test.json",
//"outputTemplate": "{Timestamp:G} {Message}{NewLine:1}{Exception:1}",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss}|{Level} => CorrelationID:{CorrelationID} => RequestId:{RequestId} => RequestPath:{RequestPath} => {SourceContext}{NewLine} {Message}{NewLine}{Exception}",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
]
}
Program
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.WithProperty("App Name", "example")
.CreateLogger();
try
{
Log.Information("Starting host");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
Upvotes: 1
Views: 1441
Reputation: 707
I don't know how to add the configuration below to the appsettins.json file but here you are:
//...
using Amazon.Runtime;
using AWS.Logger;
using AWS.Logger.SeriLog;
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Context;
using Serilog.Formatting.Json;
// ...
// create a config object from the appsettings.json file
private static IConfiguration Configuration { get; set; } = new
ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
// ...
// you can get these configs from your appsettings.json
var awsS3sKey = Configuration["aws:access.key"];
var awsS3Secret = Configuration["aws:secret.key"];
var logGroupName = Configuration["aws:log.group.name"];
var region = Configuration["aws:log.region"];
// create your AWS credential
var credentials = new BasicAWSCredentials(awsS3sKey, awsS3Secret);
// create a logger for AWS Cloudwatch
var awsConfiguration = new AWSLoggerConfig
{
Region = region,
LogGroup = logGroupName,
Credentials = credentials
};
// create a serilog config
var config = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.WriteTo.AWSSeriLog(awsConfiguration, null, new JsonFormatter());
// logger is ready
Log.Logger = config.CreateLogger();
// ...
If you know how to add these settings to the appsettings file, please let me know.
By the way, I use these nuget packages below:
Upvotes: 1