-
Notifications
You must be signed in to change notification settings - Fork 49
ILogger implementation for ApplicationInsights #239
ILogger implementation for ApplicationInsights #239
Conversation
Trying to collect all the issues which are logged across various repos. Please feel free to reference other issues here. microsoft/ApplicationInsights-aspnetcore#491 |
@RamjotSingh we have an ongoing conversation with Asp.Net Core team on the high-level design of this feature . There are many things to consider: scopes support and meaning in AppInsights, sampling, common ids. We also have ILogger implementation in AspNEtCore repo: https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/ed794ae3a0150715d76432c7417aa93c334126af/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs So could we hold this on until we finalize the design? |
@lmolkova Oh ok that's cool. I thought that this was sitting on cold plate so I ended up picking it up. The ILogger implementation I have done is actually based on what @SergeyKanzhelev wrote in his private branch (which was in turn based on AI-ASPNetCore repo). If you feel this is getting picked up I can close the PR for now. Let me know what you think. |
@RamjotSingh Thanks for your effort here - we definitely want to take it. We may change implementation later based on discussions with asp.net core team, but moving this to logging is the right thing to do now! |
Once we merge this and ship a new nuget package from this repo (Microsoft.ApplicationInsights.ILoggerProvider or Microsoft.Extensions.Logging.ApplicationInsights), the extension methods on application insights asp.net core sdk can be marked obsolete to be removed in the next major version bump. It'll still contain the duplicate code for ApplicationInsightsLogger etc. User who want to use ILogger with ApplicationInsights should use this new package, and new extension methods. |
…aches the logger instances as they are already called by LoggerFactory
I'm hope it would look something like this: var services = new ServiceCollection();
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddApplicationInsights("--YourAIKeyHere--");
});
var serviceProvider = services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("MyCategory");
using (logger.BeginScope("Test"))
{
logger.LogInformation("Logger is working");
} in 3.0 it'll look like this: var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddApplicationInsights("--YourAIKeyHere--");
});
var logger = loggerFactory.CreateLogger("MyCategory");
using (logger.BeginScope("Test"))
{
logger.LogInformation("Logger is working");
} More advanced options should look like this: var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddApplicationInsights(options =>
{
options.InstrumentationKey = "--YourAIKeyHere--";
});
});
var logger = loggerFactory.CreateLogger("MyCategory");
using (logger.BeginScope("Test"))
{
logger.LogInformation("Logger is working");
} Generic Host (3.0 APIs): var host = Host.CreateHostBuilder()
.ConfigureLogging(logging =>
{
logging.AddApplicationInsights("--YourAIKeyHere--");
})
.Build();
host.RunAsync(); |
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left few comments.. Most importantly - i cannot see how a user would setup application insights to log only logs above a given level ? Loglevel seems missing..
Other than that (and few minor comments), looks good!
@cijothomas Here is how someone can configure ApplicationInsights to only log above a specific level. loggingBuilder.AddFilter((logLevel) =>
{
if (logLevel > LogLevel.Information)
{
return true;
}
else
{
return false;
}
}).AddApplicationInsights(); I think this is what @pakrym talked about when referring to Filtering. |
Yes filtering is built into the factory as a first class citizen. |
…d is not yet supporting them
…github.com/RamjotSingh/ApplicationInsights-dotnet-logging into ramjsing/develop-ApplicationInsightsLogger
@RamjotSingh we fixed build issue (#243) and are going to ship it in next beta release. |
@RamjotSingh thanks a lot for the contribution! 🥇 |
Original discussion at - microsoft/ApplicationInsights-aspnetcore#749
C# Console program sample
Packages installed