gpt4 book ai didi

c# - 带有 AppInsights 的 SeriLog 不写入 AppInsights

转载 作者:太空狗 更新时间:2023-10-29 23:49:21 25 4
gpt4 key购买 nike

我在 .NET CORE 2.0 项目上使用 Serilog 和 appinsights 编写日志。

我已经按如下方式配置了 SeriLog,

var loggerConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.WithDemystifiedStackTraces();

并按如下方式写入 appInsights,

 loggerConfiguration = loggerConfiguration.WriteTo.ApplicationInsightsTraces(appInsightsIntrumentationKey, serilogLevel)
.WriteTo.RollingFile(Path.Combine(contentRoot, "Logs/log-{Date}.log"), retainedFileCountLimit: 14);

我在日志文件夹中看到生成的日志,但我在 appInsights 中看不到任何内容。

我做错了什么?

最佳答案

我试过了as documented

    "ApplicationInsights": {
"InstrumentationKey": "7334f234-42c9-4a1f-b35d-4969d48020d4",
"EnableAdaptiveSampling": false,
"EnablePerformanceCounterCollectionModule": false
},
"Serilog": {
"WriteTo": [
{ "Name": "Console" },
{
"Name": "ApplicationInsights",
"Args": {
"instrumentationKey": "7334f234-42c9-4a1f-b35d-4969d48020d4",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
}

它适用于 Azure 应用服务和开发计算机

程序.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // added to embed serilog
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

启动.cs

public Startup(IConfiguration configuration)
{
Configuration = configuration;
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
}

示例 Controller :

    [Route("[controller]")]
[ApiController]
public class BadValuesController : ControllerBase
{
private readonly ILogger<BadValuesController> _logger;
public BadValuesController(ILogger<BadValuesController> logger)
{
_logger = logger;
_logger.LogDebug("ctor");
}

[HttpGet]
public IEnumerable<WeatherForecast> GetData()
{
_logger.LogWarning("going to raise 6 from local app");
throw new NotImplementedException();
}
}

无需重建应用程序,因为未执行任何 services.AddApplicationInsightsTelemetry();。您可以将 dll 文件复制到您的应用程序并调整配置以打开 AppInsights 登录。

但是你不have TelemetryClient instance如果没有源代码修改。因此您需要等待一段时间才能选择应用洞察日志。

我已经安装了 NuGet 包

  • Microsoft.ApplicationInsights.AspNetCore
  • Serilog.AspNetCore
  • Serilog.Enrichers.Environment
  • Serilog.Enrichers.Process
  • Serilog.Sinks.ApplicationInsight

关于c# - 带有 AppInsights 的 SeriLog 不写入 AppInsights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48922867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com