简体   繁体   English

如何使用 Serilog 将库日志记录到 AWS CloudWatch?

[英]How to log library logs to AWS CloudWatch using Serilog?

Currently, I am working on ASP.NET 6 Web API and as a logger We use Serilog to log all the necessary details to cloudwatch and it's working fine.目前,我正在研究 ASP.NET 6 Web API 并作为记录器,我们使用 Serilog 将所有必要的详细信息记录到 cloudwatch,它工作正常。 Now I need to add library logs such as AWS errors to cloudwatch.现在我需要将诸如 AWS 错误之类的库日志添加到 cloudwatch。 Currently there is an option for that in config file but it only saves logs as a file which results a No space left on device: '/app/Logs/serilog-aws-errors.txt' and the details in the file didn't appear on cloudwatch logs.目前在配置文件中有一个选项,但它只将日志保存为文件,导致设备上没有剩余空间:'/app/Logs/serilog-aws-errors.txt'并且文件中的详细信息没有出现在 cloudwatch 日志中。

This is the appsettings data I use,这是我使用的 appsettings 数据,

"Serilog": {
"Using": [ "AWS.Logger.SeriLog", "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
  { "Name": "AWSSeriLog" },
  { "Name": "Console" },
  {
    "Name": "File",
    "Args": {
      "path": "Logs/webapi-.txt",
      "rollingInterval": "Day"
    }
  }
],
"Region": "eu-west-2",
"LogGroup": "/development/serilog",
"LibraryLogFileName": "Logs/serilog-aws-errors.txt"
}

I need to know that there is a way to log the details in serilog-aws-errors.txt to AWS cloudwatch or S3 bucket.我需要知道有一种方法可以将 serilog-aws-errors.txt 中的详细信息记录到 AWS cloudwatch 或 S3 存储桶。

This depends a lot on where in AWS you are trying to deploy your service.这在很大程度上取决于您尝试在 AWS 中部署服务的位置。 In ECS or Fargate you can log directly to the console.在 ECS 或 Fargate 中,您可以直接登录到控制台。 This would be a snippet of the container Definition:这将是容器定义的一个片段:

    "containerDefinitions": [
      {
        "logConfiguration": {
          "logDriver": "awslogs",
          "options": {
            "awslogs-group": "/dev/ecs/my-api-logs-here",
            "awslogs-region": "us-east-1",
            "awslogs-stream-prefix": "ecs"
          }
        },

With the configuration above you only need the Serilog.Sinks.Console and everything will log without a special AWS sink.使用上面的配置,您只需要Serilog.Sinks.Console并且所有内容都将在没有特殊 AWS 接收器的情况下记录。 To write to the console you can just use要写入控制台,您可以使用

                loggerConfiguration.WriteTo.Async(a =>
                {
                    a.Console(new JsonFormatter());
                });

When deployed to Fargate or ECS, these console logs will appear in your CloudWatch logs.当部署到 Fargate 或 ECS 时,这些控制台日志将出现在您的 CloudWatch 日志中。 No additional sink is necessary.不需要额外的水槽。 Lambda logs have a similar setup. Lambda 日志具有类似的设置。 See: https://docs.aws.amazon.com/lambda/latest/dg/csharp-logging.html for more details.有关更多详细信息,请参阅: https://docs.aws.amazon.com/lambda/latest/dg/csharp-logging.html

If you want to use the Serilog.Sinks.AwsCloudWatch , it does have some nice features, but the setup is a little different.如果您想使用Serilog.Sinks.AwsCloudWatch ,它确实有一些不错的功能,但设置略有不同。 You probably won't want to log to the console or your file sink at all.您可能根本不想登录到控制台或文件接收器。 Instead, you'll just log directly to CloudWatch.相反,您只需直接登录到 CloudWatch。 You'll want to set it up according to their instructions on Github: https://github.com/Cimpress-MCP/serilog-sinks-awscloudwatch .您需要根据 Github 上的说明进行设置: https://github.com/Cimpress-MCP/serilog-sinks-awscloudwatch You can get this up and running in your local environment and then set your app settings up in a way that this only runs when deployed to AWS, and locally you still use the console or file settings.您可以在本地环境中启动并运行它,然后以仅在部署到 AWS 时运行的方式设置您的应用程序设置,并且在本地您仍然使用控制台或文件设置。

  var options = new CloudWatchSinkOptions
  {
    // the name of the CloudWatch Log group for logging
    LogGroupName = logGroupName,

    // the main formatter of the log event
    TextFormatter = formatter,
    
    // other defaults defaults
    MinimumLogEventLevel = LogEventLevel.Information,
    BatchSizeLimit = 100,
    QueueSizeLimit = 10000,
    Period = TimeSpan.FromSeconds(10),
    CreateLogGroup = true,
    LogStreamNameProvider = new DefaultLogStreamProvider(),
    RetryAttempts = 5
  };

  // setup AWS CloudWatch client
  var client = new AmazonCloudWatchLogsClient(myAwsRegion);

  // Attach the sink to the logger configuration
  Log.Logger = new LoggerConfiguration()
    .WriteTo.AmazonCloudWatch(options, client)
    .CreateLogger();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM