简体   繁体   English

如何配置 Serilog 接收器以记录到 CloudWatch

[英]How to configure a Serilog sink for logging to CloudWatch

I would appreciate some guidance in setting up a Serilog sink for AWS CloudWatch with .NET Core.我将不胜感激为使用 .NET Core 的 AWS CloudWatch 设置 Serilog 接收器的一些指导。 I'm using appsettings.json for configuration but I am unable to put the settings in the logger.我正在使用 appsettings.json 进行配置,但我无法将设置放入记录器中。 When trying to write log information to CloudWatch this error appears:尝试将日志信息写入 CloudWatch 时出现此错误:

An error occurred while starting the application.
AmazonClientException: No RegionEndpoint or ServiceURL configured

Amazon.Runtime.ClientConfig.Validate() in ClientConfig.cs, line 446

AmazonClientException: No RegionEndpoint or ServiceURL configured

Amazon.Runtime.ClientConfig.Validate() in ClientConfig.cs
Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config) in AmazonServiceClient.cs
AWS.Logger.Core.AWSLoggerCore..ctor(AWSLoggerConfig config, string logType) in AWSLoggerCore.cs
AWS.Logger.SeriLog.AWSSink..ctor(AWSLoggerConfig loggerConfiguration, IFormatProvider iFormatProvider, ITextFormatter textFormatter) in AWSSink.cs
AWS.Logger.SeriLog.AWSLoggerSeriLogExtension.AWSSeriLog(LoggerSinkConfiguration loggerConfiguration, IConfiguration configuration, IFormatProvider iFormatProvider, ITextFormatter textFormatter) in AWSLoggerSeriLogExtension.cs
...
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions+InjectApiVersionRoutePolicy+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass4_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

My code is as follows:我的代码如下:

In Startup.cs:在 Startup.cs 中:

public Startup(IHostingEnvironment env)
{
    Configuration = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables()
        .Build();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(Configuration.GetSection("AWS.Logging"))
        .WriteTo.AWSSeriLog(Configuration)
        .CreateLogger();

    app.UseMvc();
}

My appsettings.json:我的 appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AWS.Logging": {
    "Region": "eu-west-1",
    "LogGroup": "MyLogGroup",
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "AllowedHosts": "*"
}

Credentials for AWS from my developer machine are set by using environment variables:我的开发人员机器上的 AWS 凭据是使用环境变量设置的:

AWS_ACCESS_KEY_ID=xxxxxxxxxx
AWS_DEFAULT_REGION=eu-west-1
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxx

Just in case anyone has the same problem, this appsettings.json solved it:以防万一有人遇到同样的问题,这个 appsettings.json 解决了它:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "Serilog": {
    "Using": [ "AWS.Logger.SeriLog" ]
    "Region": "eu-west-1",
    "LogGroup": "MyLogGroup",
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "AllowedHosts": "*"
}

I don't think the previous solution is complete, it misses the WriteTo part, the minimal should look like this:我不认为前面的解决方案是完整的,它遗漏了WriteTo部分,最小的应该是这样的:

"Serilog": {
    "Using": [
      "AWS.Logger.SeriLog"
    ],
    "LogGroup": "MyLogGroup",
    "Region": "eu-west-1",
    "WriteTo": [
      {
        "Name": "AWSSeriLog",
        "Args": {
          "textFormatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
        }
      }
    ]
  }

This requires Serilog.Formatting.Compact library to be installed (I recommend using it, much neater log records), you can also use the default:这个需要安装Serilog.Formatting.Compact库(推荐使用,日志记录更整洁),也可以使用默认的:

"textFormatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"

This setup is utilising the AWS supplied library AWS.Logger.Serilog , do not forget to add it to your NuGet packages.此设置使用 AWS 提供的库AWS.Logger.Serilog ,不要忘记将其添加到您的 NuGet 包中。

Also, if you are not running the app inside the AWS, the AWS credentials are to be provided.此外,如果您没有在 AWS 中运行该应用程序,则需要提供 AWS 凭证。

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

相关问题 带有 Serilog 控制台接收器的 Cloudwatch 显示在带有 do.net 的单个 cloudwatch 条目中 lambda - Cloudwatch with Serilog console sink showing in single cloudwatch entry with dotnet lambda Serilog中如何根据开发环境将日志写入特定的sink? - How to write logs to a specific sink depending on the developement environment in Serilog? 如何更改 cloudwatch 代理日志区域? - How to change the cloudwatch agent logging region? GCP 日志接收器不导出 - GCP Logging Sink not exporting AWS fargate 记录到 cloudwatch 会导致 NAT 流量 - 如何避免 - AWS fargate logging to cloudwatch causes NAT traffic - how to avoid 如何将 CloudWatch 警报配置为每 X 分钟评估一次 - How to configure a CloudWatch alarm to evaluate once every X minutes GCP 日志记录路由器接收器更新的权限错误 - permission error for GCP logging router sink update 如何在 VPC 中配置 AWS Lambda 以便能够登录到另一个 Cloudwatch 日志组? - How do I configure an AWS Lambda in a VPC to be able to log to another Cloudwatch log group? 来自 Cloud Logging 接收器的重复 BigQuery 行 - Duplicated BigQuery rows from Cloud Logging sink 重新创建/重新附加 AWS Lambda 控制台日志记录到 CloudWatch - Recreating/reattaching AWS Lambda console logging to CloudWatch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM