简体   繁体   中英

SeriLog with AppInsights not writing to AppInsights

I am writing logs using Serilog with appinsights on an .NET CORE 2.0 project.

i have configured SeriLog as follows,

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

and writing to appInsights as follows,

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

i see the log generated inside the logs folder, but i dont see anything in appInsights.

what i am doing wrong?

Make sure to install the SDK prior to the Serilog ApplicationInsights sink. If you don't have the SDK, it seems to work but you will end up without any messages in ApplicationInsights.

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

I've tried to do it 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"
                }
            }
        ]
    }

it works from both Azure App Service and dev computer

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();
}

sample 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();
        }
    }

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.

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.

I've installed NuGet packages

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM