[英]SeriLog with AppInsights not writing to AppInsights
我正在使用 Serilog 和 .NET CORE 2.0 项目的 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 中没有看到任何内容。
我在做什么错?
确保在 Serilog ApplicationInsights 接收器之前安装 SDK。 如果您没有 SDK,它似乎可以工作,但您最终将在 ApplicationInsights 中没有任何消息。
https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore/
我已经尝试按照文档进行操作
"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>();
});
启动文件
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
}
样品控制器:
[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();
不需要重建应用程序services.AddApplicationInsightsTelemetry();
被执行。 您可以将dll
文件复制到您的应用程序并调整配置以打开 AppInsights 登录。
但是,如果没有源代码修改,则您没有TelemetryClient
实例。 所以你必须等待一段时间才能选择应用洞察日志。
我已经安装了 NuGet 包
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.