[英]ILogger to Application Insights
使用Microsoft.ApplicationInsights.AspNetCore v2.6.1
v2.6.1 和 .net core v2.2.2 我可以看到 Azure Application Insight Live Metric Stream 中的遥测数据,但我没有看到我尝试使用ILogger
在Startup.cs
记录的条目Startup.cs
或在控制器中。
我已经在Program.cs
和Startup.cs
尝试了.UseApplicationInsights()
和WebHost.CreateDefaultBuilder
,但无济于事。
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()}");
更新:
如果您安装了最新的软件包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
}
测试结果(所有日志都发送到应用洞察):
请参阅带有类似问题的问题以及我对此的回答: Log4Net和Application Insights-没有数据通过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.