我今天遇到了同样的情况,我发现自定义格式化程序(默认情况下没有人设置)它用于设置日志的写入方式,因此它将出现在 AWS 日志中。
您可以开始使用 Serilog 提供的界面创建一个简单的格式化程序,并根据您的情况进行调整。
public class AWSTextFormatter : ITextFormatter
{
public void Format(LogEvent logEvent, TextWriter output)
{
output.Write("Timestamp - {0} | Level - {1} | Message {2} {3}", logEvent.Timestamp, logEvent.Level, logEvent.MessageTemplate, output.NewLine);
if (logEvent.Exception != null)
{
output.Write("Exception - {0}", logEvent.Exception);
}
}
}
Using it
var customFormatter = new AWSTextFormatter();
var options = new CloudWatchSinkOptions
{
// the name of the CloudWatch Log group for logging
LogGroupName = logGroupName,
// the main formatter of the log event
TextFormatter = customFormatter,
// other defaults defaults
MinimumLogEventLevel = LogEventLevel.Information,
BatchSizeLimit = 100,
QueueSizeLimit = 10000,
Period = TimeSpan.FromSeconds(10),
CreateLogGroup = true,
LogStreamNameProvider = new DefaultLogStreamProvider(),
RetryAttempts = 5
};
在该方法中,您可以访问 LogEvent 并获取所需的所有信息。这个和他们在lib中使用的类似,写了事件、异常和一些细节。
建议在本地测试自定义格式化程序,将日志写入某个文件中
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.RollingFile(customFormatter, Path.Combine(env.ContentRootPath, "Logs", "Log-{Date}.txt"))
.WriteTo.AmazonCloudWatch(options, client)
.CreateLogger();
日志显示的内容:
Timestamp - 1/9/2019 11:52:11 AM -02:00 | Level - Fatal | Message PROBLEM STARTING
APPLICATION
Exception - Couchbase.Configuration.Server.Serialization.BootstrapException: Could not
bootstrap - check inner exceptions for details.