带有AppInsights的SeriLog不写入AppInsights

时间:2018-02-22 08:37:52

标签: c# .net-core azure-application-insights serilog

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

我按如下方式配置了SeriLog,

onEdit

并写入appInsights,如下所示,

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

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

我做错了什么?

2 个答案:

答案 0 :(得分:0)

确保在Serilog ApplicationInsights接收器之前安装SDK。如果您没有SDK,它似乎可以工作,但最终会在ApplicationInsights中没有任何消息。

https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore/

答案 1 :(得分:0)

我已经尝试过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 App Service和开发计算机中使用

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软件包 Microsoft.ApplicationInsights.AspNetCore Serilog.AspNetCore Serilog.Enrichers.Environment Serilog.Enrichers.Process Serilog.Sinks.ApplicationInsight