Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

amazon web services - AWS CloudWatch - How to remove LogLevel and Class that logged in default for .NET Core?

I have success logged the message from my .NET Core API to AWS CloudWatch.

However, I found that the message will automatically consist LogLevel and the class that prints the logs, as below:

[Information] Infra.Logging: 
{
    "id": "001",
    "message": "Received Get Data Request",
    "logLevel": "Information"
}

I want to get rid of the [Information] & Infra.Logging from the message. I tried to configure the appsettings as below:

"Logging": {
 "IncludeLogLevel": false,
 "IncludeCategory": false,
 "IncludeNewline": false,
 "IncludeException": false,
 "IncludeEventId": false,
 "IncludeScopes": false,
 "LogLevel": {
   "Default": "Trace",
   "System": "Information",
   "Microsoft": "Information"
 }
}

But it is not working. Is there any way that can remove the two field that's added automatically into @message in the CloudWatch?

Here is how I logged the message:

var loggerConfig = new AWSLoggerConfig
{
     LogGroup = logGroup,
     Region = region
};

var logFactory = new LoggerFactory();
logFactory.AddAWSProvider(loggerConfig);

var logger = logFactory.CreateLogger<Infra.Logging>();
logger.LogInformation(logMessageString);

The logMessageString will be the string convert from JSON:

{
    "id": "001",
    "message": "Received Get Data Request",
    "logLevel": "Information"
}

The reason I want to remove is because I wish to query my log in Insight like:

fields @timestamp, @message
| filter id="001"
| sort @timestamp desc

This query is not working now so I figured might be the default logged [Information] & Infra.Logging issue.

Appreciates any helps!

question from:https://stackoverflow.com/questions/65948566/aws-cloudwatch-how-to-remove-loglevel-and-class-that-logged-in-default-for-ne

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With your existing logging i.e. with [Information] Infra.Logging: after that the further log message. You can use parse command as shown below. For more details about parse command, please refer this document from AWS.

Assumption: You have following logging structure; enter image description here

One of possible solution is to execute aws cloud watch insight query as follows;

fields @message | parse @message '[*] Infra.Logging: {"id": "*"' as @loggingType, @some_id | filter @loggingType='Information' and @some_id like '2' | sort @timestamp desc

Output after execution aws cloud watch insight query is as follows; enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...