问题描述
当我通常使用Fargate在ECS中运行任务时,STDOUT会自动重定向到cloudwatch,并且可以轻松找到此应用程序日志.
When I usually run a task in ECS using Fargate, the STDOUT is redirected automatically to cloudwatch and this application logs can be found without any complication.
例如在C#中进行澄清:
To clarify, for example, in C#:
Console.WriteLine("log to write to CloudWatch")
当我将ECS与Fargate或Lambda函数一起使用时,该输出会自动重定向到CloudWatch日志
That output is automatically redircted to CloudWatch logs when I use ECS with Fargate or Lambda functions
我想使用EC2进行同样的操作.
I would like to do the same using EC2.
将ECS与EC2结合使用的第一印象是,它不像Fargate那样自动.我说的对吗?
The first impression using ECS with EC2 is that this is not as automatic as Fargate. Am I right?
在寻找信息时,我发现以下内容(其他较早的问题或帖子除外):
Looking for a information I have found the following (apart of other older question or post):
-
在此问题中,该问题指的是AWS博客中的旧帖子,因此这可能已经过时了.
In this question refers to an old post from the AWS blog, sothis could be obsolete.
在此 AWS页面中,它们描述了您需要执行的几个步骤在您的EC2上安装一些实用程序
In this AWS page, they describe a few steps where you need toinstall some utilities to your EC2
因此,总而言之,当我以与Fargate相同的方式将ECS与EC2一起使用时,有什么方法可以在cloudwatch中查看STDOUT吗?
So, summarizing, is there any way to see the STDOUT in cloudwatch when I use ECS with EC2 in the same way Fargate does?
推荐答案
这就是我的方法.
使用块:AWS.Logger.AspNetCore
Using the nugget: AWS.Logger.AspNetCore
使用示例:
static async Task Main(string[] args)
{
Logger loggerObj = new Logger();
ILogger<Program> logger = await loggerObj.CreateLogger("test", "eu-west-1");
logger.LogInformation("test info");
logger.LogError("test error");
}
public async Task<ILogger<Program>> CreateLogger(string logGroup, string region)
{
AWS.Logger.AWSLoggerConfig config = new AWS.Logger.AWSLoggerConfig();
config.Region = region;
config.LogStreamNameSuffix = "";
config.LogGroup = logGroup;
LoggerFactory logFactory = new LoggerFactory();
logFactory.AddAWSProvider(config);
return logFactory.CreateLogger<Program>();
}
这篇关于ECS日志:Fargate与EC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!