如何为Cloudwatch创建自定义文本格式器?

时间:2019-01-08 09:37:43

标签: c# .net-core amazon-cloudwatch serilog text-formatting

我不明白如何为提到的Amazon Cloudwatch创建自定义文本格式器:

var formatter = new MyCustomTextFormatter();

我正在尝试将Serilog日志而不是本地硬盘写入Amazon CloudWatch。为此,我要遵循此回购协议:

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;
}

能够将Serilogs写入Cloudwatch。

2 个答案:

答案 0 :(得分:1)

我今天遇到的情况也是这样,我发现自定义格式化程序(默认情况下未设置该自定义格式化程序)用于设置日志的写入方式,并因此将其显示在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);
        }
    }
}

使用

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.

答案 1 :(得分:1)

您也可以使用 Serilog 的默认 Json 格式化程序。

using Serilog.Formatting.Json;

然后:

TextFormatter = new JsonFormatter(Environment.NewLine);