none
can't capture traces when instrumenting dotnet core 2 console app

    Question

  • I can see the information from TelemetryClient but I can`t those captured by ILogger.
    public class Program
    {
    	static IConfiguration _configuration;
    
    	static void Main(string[] args)
    	{
    		var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    
    		IConfigurationBuilder builder = new ConfigurationBuilder();
    
    		_configuration = Configure(builder, environmentName);
    
    		var instrumentationKey = _configuration["ApplicationInsights:InstrumentationKey"];
    
    		ILoggerFactory loggerFactory = new LoggerFactory();
    
    		var config = new JobHostConfiguration();
    		config.DashboardConnectionString = "xxxx";
    		config.LoggerFactory = loggerFactory
    			.AddApplicationInsights(instrumentationKey, null)
    			.AddConsole();
    
    		var host = new JobHost(config);
    		host.CallAsync(typeof(Functions).GetMethod("ProcessMethod"));
    		host.RunAndBlock();            
    	}
    
    	private static IConfiguration Configure(IConfigurationBuilder config, string environmentName)
    	{
    		return config
    			.SetBasePath(Directory.GetCurrentDirectory())
    			.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    			.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
    			.AddEnvironmentVariables()
    			.Build();
    	}
    
    }
    
    
    
    public class Functions
    {
    	[NoAutomaticTrigger]
    	public static async Task ProcessMethod(ILogger log)
    	{
    		TelemetryClient client = new TelemetryClient();
    
    		client.InstrumentationKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
    
    		while (true)
    		{
    			// Do something.      
    
    			log.LogTrace($"ILogger.LogTrace - ILogger.LogTrace");
    			client.TrackTrace($"TrackTrace TelemetryClient - TrackTrace TelemetryClient");
    			client.Flush();
    
    			Console.WriteLine("This ProcessMethod method is executed");			await Task.Delay(TimeSpan.FromMinutes(1));
    		}
    	}
    }

    any tips?

    Thiago Custodio | Software Engineer
    Blog
    Twitter
    Meu livro de Azure

    Friday, November 23, 2018 5:36 PM

All replies

  • Reading traces from ILogger in Console App is not currently supported. It is supported in regular Asp.Net Core apps only.

    The following is the issue tracking this: We are likely to move the Ilogger adaptor outside of the Asp.net Core package, so as to enable using it with console apps. No ETA yet, but likely in the next 2-3 months.

    https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/507

    Tuesday, November 27, 2018 1:45 AM