Reputation: 120
I an trying to log details of my function app into Application Insights.
My basic code:
public class AzureAppInsightsExplore
{
private readonly ILogger<AzureAppInsightsExplore> logger;
public AzureAppInsightsExplore(ILogger<AzureAppInsightsExplore> logger)
{
this.logger = logger;
}
[FunctionName("AzureAppInsightsExplore")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) //Can we directly use log
{
// Unable to find this log in trace but in live metrics it is shown.
logger.LogInformation("C# HTTP trigger function processed a request.");
int a = 0, b = 0;
//Unhandled exception
int c = a / b;
return new OkObjectResult(string.Empty);
}
}
Host.Json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
Here are few strange things I have noticed (not sure where I am doing wrong).
Unhandled Exception is logging twice in Application Insights.
Any specific reason to inject ILogger<ClassName> logger
when function run method is having ILogger log
by default.
Main concern is that I am seeing a lot of unwanted logs in trace table, where I am just expecting it would have information that I log in code with log.LogXXX(message)
.
Is there a way that I can stop loading unwanted data Trace
table, because it would increase the cost.
I am not see those logs messages that I am logging from code (I have tried after 10 mins, as it might take some time to load into trace tables) , but I can see those logs in Live metrics.
Can someone suggest me on the above, It would be really helpful.
Kind regards.
Upvotes: 4
Views: 1026
Reputation: 11253
Thanks @Hooman Bahreini, According to SO-Thread it says,
ILogger: is responsible to write a log message of a given Log Level. ILoggerProvider: is responsible to create an instance of
ILogger
(you are not supposed to useILoggerProvider
directly to create a logger). ILoggerFactory: you can register one or moreILoggerProvider
s with the factory, which in turn uses all of them to create an instance ofILogger
.ILoggerFactory
holds a collection ofILoggerProviders
.
We can able to see logging messages you need give correct format in quotations and column name too:
Upvotes: 0