问题描述
我正在使用Serilog和.NET CORE 2.0项目上的appinsights编写日志.
I am writing logs using Serilog with appinsights on an .NET CORE 2.0 project.
我已经按照以下方式配置了SeriLog,
i have configured SeriLog as follows,
var loggerConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.WithDemystifiedStackTraces();
并按如下所示写入appInsights
and writing to appInsights as follows,
loggerConfiguration = loggerConfiguration.WriteTo.ApplicationInsightsTraces(appInsightsIntrumentationKey, serilogLevel)
.WriteTo.RollingFile(Path.Combine(contentRoot, "Logs/log-{Date}.log"), retainedFileCountLimit: 14);
我看到在logs文件夹中生成的日志,但是在appInsights中看不到任何东西.
i see the log generated inside the logs folder, but i dont see anything in appInsights.
我做错了什么?
推荐答案
我已经尝试过已记录
"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 App Service中运行,也可以在开发计算机中运行
it works from both Azure App Service and dev computer
Program.cs:
Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // added to embed serilog
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Startup.cs
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登录.
There is no need to rebuild application since no services.AddApplicationInsightsTelemetry();
is executed. You may just copy dll
files to your application and adjust config to turn the AppInsights logging on.
但是您不如果没有源代码修改,则具有 TelemetryClient
实例.因此,您必须等待一段时间才能选择应用洞察日志.
However you do not have TelemetryClient
instance if there is no source code modifications. So you have to wait a while before can select app insights log.
我已经安装了NuGet软件包
I've installed NuGet packages
- Microsoft.ApplicationInsights.AspNetCore
- Serilog.AspNetCore
- Serilog.Enrichers.Environment
- Serilog.Enrichers.Process
- Serilog.Sinks.ApplicationInsight
这篇关于带有AppInsights的SeriLog未写入AppInsights的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!