如何为Cloudwatch创建自定义文本格式器? [英] How to create a Custom text formatter for Cloudwatch?

查看:104
本文介绍了如何为Cloudwatch创建自定义文本格式器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不理解如何为上述提到的Amazon Cloudwatch创建自定义文本格式器:

I don't understand how to create a custom text formatter for Amazon Cloudwatch as mentioned:

var formatter = new MyCustomTextFormatter();

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

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

能够将Serilog写入Cloudwatch。

To be able to write the Serilogs to Cloudwatch.

推荐答案

我今天遇到相同的情况,我发现自定义格式化程序(默认情况下没有人设置)用于设置日志将被写入,因此将出现在AWS日志中。

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.

您可以使用Serilog提供的界面开始创建简单的格式化程序,并使其适应您的情况。

You can start creating a simple formatter using the interface that Serilog provides and adapt to the best fit in your situation.

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中使用的东西,它写了事件,异常和一些细节。

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

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.

这篇关于如何为Cloudwatch创建自定义文本格式器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆