将Microsoft.ApplicationInsights.AspNetCore v2.6.1与.net core v2.2.2结合使用时,我可以看到遥测在Azure Application Insight Live Metric Stream中进行,但不是我尝试在Startup.cs或控制器中使用ILogger记录的遥测
我在Proagram.cs和Startup.cs中都尝试对WebHost.CreateDefaultBuilder使用.UseApplicationInsights()
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()}");
答案 0 :(得分:1)
更新:
如果您安装了最新的软件包Microsoft.Extensions.Logging.ApplicationInsights
(2.9.1),则可以遵循此doc。
在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
}
测试结果(所有日志均发送给应用程序见解):
答案 1 :(得分:0)
请参阅带有类似排序问题的问题及其答案:Log4Net and Application Insights-no data is coming through