我正在使用Serilog和appinsights在一个.NET核心2.0项目上编写日志。
我已经按如下方式配置了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);我在logs文件夹中看到了生成的日志,但在appInsights中看不到任何内容。
我做错了什么?
发布于 2019-05-21 23:20:56
确保在Serilog ApplicationInsights接收器之前安装SDK。如果你没有SDK,它似乎可以工作,但你最终会在ApplicationInsights中没有任何消息。
https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore/
发布于 2020-11-18 20:28:14
我试过了,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应用程序服务和开发计算机上运行
Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // added to embed serilog
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});Startup.cs
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();,因此无需重新构建应用程序。您只需将dll文件复制到您的应用程序中,并调整配置以打开AppInsights日志。
但是,如果没有对源代码进行修改,就不会使用have TelemetryClient instance。所以你必须等待一段时间才能选择应用程序洞察日志。
我已经安装了NuGet包
https://stackoverflow.com/questions/48922867
复制相似问题