c# - AppInsights 仅保存遥测数据,但不使用 IIS 上托管的 .Net Core Web 应用程序保存日志
问题描述
我的Program
课看起来像这样
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>();
}
和我Startup
的ConfigureServices
services.AddApplicationInsightsTelemetry(configuration["Logging:ApplicationInsights:InstrumentationKey"]);
这是我的控制器类:
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();
应用设置.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");
推荐阅读
- css - 如何让大众字体停止增长
- javascript - 使用遍历 2 个数组的循环将键/值添加到空对象中
- python - 如何在 Django 模型中创建和保存文件
- powershell - 当所有者不是管理员时,“Set-Acl”不起作用
- ruby-on-rails - 如何在 Rspec 中为哈希创建模拟
- python - 有些人可以使用相同的内核参数在 tensorflow 或 keras 中重复使用 Con2d
- angular - 如何处理从 Angular 中的 http 调用返回的服务器端验证错误?
- javascript - 通过猫鼬中的功能处理许多领域的问题
- docker - Docker 构建 Kitura Sqift 容器 - 找不到 Shim.h mysql.h 文件
- opentbs - OpenTBS - 检查复选框字段中的各个选项以插入每个选项的条件内容