使用带有Microsoft.ApplicationInsights.AspNetCore v2.6.1核心版本2.2.2的.net,我可以看到Azure应用程序Insight Live中的遥测,但我没有看到试图在Startup.cs中或控制器中用ILogger登录的条目。
我尝试过在.UseApplicationInsights()中使用WebHost.CreateDefaultBuilder,在Program.cs中,也在类似的Startup.cs中,但是都没有效果。
services.AddApplicationInsightsTelemetry( options => {
options.EnableDebugLogger = true;
});我看到传入的请求和请求的故障率,但是没有日志条目
this.logger.Log(LogLevel.Error, $"Test Error {Guid.NewGuid().ToString()}");
this.logger.LogTrace($"Test Trace {Guid.NewGuid().ToString()}");
this.logger.LogInformation($"Test Information {Guid.NewGuid().ToString()}");
this.logger.LogWarning($"Test Warning {Guid.NewGuid().ToString()}");
this.logger.LogCritical($"Test Critical {Guid.NewGuid().ToString()}");
this.logger.LogError($"Test Error{Guid.NewGuid().ToString()}");
this.logger.LogDebug($"Test Debug {Guid.NewGuid().ToString()}");发布于 2019-02-21 09:49:14
更新
如果安装了最新的包Microsoft.Extensions.Logging.ApplicationInsights (2.9.1),则可以遵循此文档。
在program.cs中:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging=> {
logging.AddApplicationInsights("your_insturmentation_key");
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace); #you can set the logLevel here
});
}然后在controller.cs中:
public class HomeController : Controller
{
ILogger<HomeController> Logger { get; set; }
TelemetryClient client = new TelemetryClient();
public HomeController(ILogger<HomeController> logger)
{
this.Logger = logger;
}
public IActionResult Index()
{
Logger.LogTrace("0225 ILogger: xxxxxxxxxxxxxxxxxxxxxxxxx");
Logger.LogDebug("0225 ILogger: debug from index page aa111");
Logger.LogInformation("0225 ILogger: infor from index page aa111");
Logger.LogWarning("0225 ILogger: warning from index page aa111");
Logger.Log(LogLevel.Error, "0225 ILogger: error from index page aa111");
return View();
}
# other code
}测试结果(所有日志都发送给应用程序洞察力):

https://stackoverflow.com/questions/54793874
复制相似问题