I don't understand how to create a custom text formatter for Amazon Cloudwatch as mentioned:
var formatter = new MyCustomTextFormatter();
I am trying to write Serilog logs to Amazon CloudWatch instead of the local hard disk. To do that I am following this repo:
https://github.com/Cimpress-MCP/serilog-sinks-awscloudwatch
private readonly ITextFormatter textFormatter;
public ILoggerFactory ConfigureLogger()
{
LoggerFactory factory = new LoggerFactory();
var logGroupName = "myLoggrouName";
var region = Amazon.RegionEndpoint.EUWest1;
var client = new AmazonCloudWatchLogsClient(region);
//var formatter = new MyCustomTextFormatter();
var options = new CloudWatchSinkOptions()
{
LogGroupName = logGroupName,
//TextFormatter = formatter,
MinimumLogEventLevel = LogEventLevel.Information,
BatchSizeLimit = 100,
QueueSizeLimit = 10000,
Period = TimeSpan.FromSeconds(10),
CreateLogGroup = true,
LogStreamNameProvider = new DefaultLogStreamProvider(),
RetryAttempts = 5
};
Log.Logger = new LoggerConfiguration()
.WriteTo.AmazonCloudWatch(options, client)
.CreateLogger();
return factory;
}
To be able to write the Serilogs to Cloudwatch.
I passed for the same situation today and what I figure out is that custom formatter - which no one is set by default - its to set how the log will be write and consequently it will appear in the AWS Logs.
You can start creating a simple formatter using the interface that Serilog provides and adapt to the best fit in your situation.
Using it
In the method you can acces the LogEvent and get all information you need. This one is similar to what they use in the lib, writing the event, exception and some detail.
A suggestion is to test the custom formatter locally writing the log in some file
What the log shows: