检测dotnet core 2控制台应用程序时无法捕获跟踪 [英] can't capture traces when instrumenting dotnet core 2 console app

查看:75
本文介绍了检测dotnet core 2控制台应用程序时无法捕获跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到来自TelemetryClient的信息,但不能看到ILogger捕获的信息.

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?


any tips?


Thiago Custodio |软件工程师
博客
Twitter
天蓝色的人

Thiago Custodio | Software Engineer
Blog
Twitter
Meu livro de Azure

推荐答案

当前不支持在Console App中从ILogger读取跟踪.仅常规Asp.Net Core应用程序支持.

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

以下是跟踪此问题的问题:我们很可能将Ilogger适配器移到Asp.net Core程序包之外,以便与控制台应用程序一起使用.尚无预计到达时间,但很可能在接下来的2-3个月内.

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

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


这篇关于检测dotnet core 2控制台应用程序时无法捕获跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆