I have a .NET Core 2.1 Console application. I have added all the Nuget packages needed.
private static IConfiguration Configuration { get; set; }
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetService<ILoggerFactory>()
.CreateLogger<Program>()
var service = serviceProvider.GetService<TestClass>();
service.TimmerTriggerTask();
}
private static void ConfigureServices(IServiceCollection services)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
services.AddApplicationInsightsTelemetry("8028437c-1111-2222-8293-2cf3f3f106a8"); //instrumentation key
services.AddLogging(builder => builder.AddConsole());
}
TestClass.cs
public class TestClass
{
private readonly ILogger<TestClass> _logger;
public TestClass(ILogger<TestClass> logger)
{
Console.WriteLine("Ctor");
_logger = logger;
}
public void TimmerTriggerTask()
{
Console.WriteLine("Timer");
//LOG BELOW IN APPLICATION INSIGHTS
_logger.LogTrace("Hello World");
_logger.LogInformation(DateTime.Now.ToString());
}
}
I need to log all the information and exceptions in Application Insights. looking to integrate loggerfactory with applicationInsights.
I am looking for something what we can do in .NET Core Web app
WebHost.CreateDefaultBuilder(args).UseApplicationInsights()
loggerFactory.AddApplicationInsights(app.ApplicationServices, defaultLogLevel);
Please help me as how do i use logger classes to log into applicationinsights.
Please provide an alternative solution if what i am doing is incorrect.
Ilogger
in Application Insights? Looks like you did the right thing. – cijothomas