问题
I am trying to add custom logs in my c# asp dot net core web api. I am able to find the api calls logs in Azure portal -> application insights -> logs.
But i am not able to find the custom logs i am entering using below code. whats the place to search for them.
public async Task Invoke(HttpContext httpContext)
{
// First, get the incoming request
var request = await FormatRequest(httpContext.Request);
// TODO: Save log to chosen datastore
_logger.LogInformation('custommessage101');
// ------
}
In log analytics query editor i used below query but it didnt fetch anything. Is it even the right place(Azure portal -> application insights -> logs) i am looking at ?
requests | search "custommessage101"
回答1:
Messages logged to the ILogger
interface end up as traces in application insights. An example query would be:
traces | where message == "custommessage101"
Another option would be the Search:
The default log level for messages to application insights is set to Warning
. As Shiraz point out, you need to set it to informational. You can do that using code as shown by Shiraz or by adjusting the appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}
回答2:
It may be that when you configured your logger you set the log level higher than information.
The following set the log level so that Information logging will be stored:
builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);
来源:https://stackoverflow.com/questions/63909107/how-to-log-custom-messages-to-azure-portal-analytics-monitoring-logs