![](/img/trans.png)
[英]Get Azure Insights Telemetry Data / Search Logs in ASP.NET Core app
[英]AppInsights is only saving telemetry data but not saving logs with a .Net Core web app hosted on an IIS
我的Program
class 看起来像这样
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostingBuilder, configBuilder) =>
{
configBuilder.SetBasePath(webHostingBuilder.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{webHostingBuilder.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
})
.ConfigureLogging((webHostingBuilder, logging) =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddDebug();
logging.AddApplicationInsights(
webHostingBuilder.Configuration.GetValue<string>("Logging:ApplicationInsights:InstrumentationKey"));
})
.UseStartup<Startup>();
}
在我的ConfigureServices
上Startup
services.AddApplicationInsightsTelemetry(configuration["Logging:ApplicationInsights:InstrumentationKey"]);
这是我的 controller class:
public class AccountController : BaseController
{
private ILogger _logger;
public AccountController(ILogger<AccountController> logger)
{ ... }
[HttpGet]
public async Task<IActionResult> Login(string returnUrl)
{
_logger.LogError("SuperError");
}
}
为了保存日志,我还需要配置其他什么吗? 在做我的例子时,我在这里看不到任何东西
你的代码看起来不错。 有时,日志可能需要一段时间才能显示在App Insights
中。 给出正确InstrumentationKey
的示例配置:
启动.cs
services.AddApplicationInsightsTelemetry();
appsettings.json
"ApplicationInsights": {
"InstrumentationKey": "putinstrumentationkeyhere"
}
WeatherForecastController.cs
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public ActionResult<IEnumerable<WeatherForecast>> Get()
{
_logger.LogInformation("WeatherForecastController Get");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.