c# - 从 Lambda 函数调用方法时,Serilog 记录器未记录
问题描述
我正在使用 serilog 进行日志记录。我在一个项目中有一个 AWS lambda 函数,它将数据记录到 cloudwatch group lambda_logs
。我oflline_method()
在另一个项目中也有一个方法,默认情况下将数据记录到 cloudwatch group offline_logs
。oflline_method()
在某些情况下,lambda 在内部调用方法。在这种情况下,所有与 lambda 相关的日志都需要登录到lambda_logs
group 中,而与 lambda 相关的日志offline_method
需要登录到offline_logs
. 但是offline_method
只有当它被 lambda 调用时,写入的日志才不会登录到 cloudwatch 组。
namespace Online
{
public class lambda
{
public async Task<KinesisFirehoseResponse> MethodA(ILambdaContext context)
{
//this will be logged into lambda_logs
context.Logger.LogLine("Logged by the method MethodA in lambda");
var firehoseResponse = new KinesisFirehoseResponse();
//Calls the oflline_method from offline project
var a = oflline_method();
return firehoseResponse;
}
}
}
namespace OfflineProject
{
public class Offline
{
private readonly ILogger<Offline> _logger;
public Offline()
{
_logger = new SerilogLoggerFactory().CreateLogger<Offline>();
}
public string oflline_method()
{
//this should be logged into offline_logs, but it is not happening only when called by lambda
_logger.LogInformation(" Logged by the method oflline_method in offline");
return "from offline";
}
}
}
依赖注入用于创建记录器对象。下面是 Startup.cs 中的配置
public Startup(IConfiguration configuration)
{
Configuration = configuration;
var options = new CloudWatchSinkOptions
{
LogGroupName = "offline_logs",
TextFormatter = new LogFormatter(),
MinimumLogEventLevel = LogEventLevel.Verbose,
BatchSizeLimit = 100,
QueueSizeLimit = 10000,
Period = TimeSpan.FromSeconds(10),
CreateLogGroup = true,
LogStreamNameProvider = new AwsCloudWatchLogStream(),
RetryAttempts = 5
};
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(new EnvironmentVariableLoggingLevelSwitch(DebugMode))
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.AmazonCloudWatch(options, new AmazonCloudWatchLogsClient())
.CreateLogger();
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
}
解决方案
推荐阅读
- terminal - 如何在 IPython shell 上更改 iTerm2 的颜色?
- java - 在 if 块中,我的 for 循环不起作用,因此 1,2,32 被烘烤而 3 没有
- html - 删除页脚 Prestashop 上的 h4 标签
- javascript - 使用 {...state} 创建只修改一个字段的新对象实例在 React JS 中不起作用
- python - 进行批量文件写入或以较小的部分写入文件是否更快?
- swift - 快速转义反斜杠无法按预期工作
- java - 转换 Observable onError 并发出一个项目
- line-breaks - 如何使用 reStructuredText 创建新行?
- docker - 在使用 docker compose 3 启动我的应用程序之前,我应该如何确定 DB 已启动
- php - Android 应用内购买验证问题:无法解析代码,返回值始终为零