serilog-contrib / serilog-sinks-applicationinsights

A Serilog sink that writes events to Microsoft Azure Application Insights
Apache License 2.0
220 stars 72 forks source link

README explanation #202

Open lucashby opened 2 years ago

lucashby commented 2 years ago

Describe your suggestion Perhaps the README that has been updated could clarify something for everyone (especially me). The section that eludes me is here,

The README mentions creating the logger after services.AddApplicationInsightsTelemetry() is called, like so:

Log.Logger = new LoggerConfiguration()
    .WriteTo.ApplicationInsights(
        serviceProvider.GetRequiredService<TelemetryConfiguration>(),
    TelemetryConverter.Traces)
    .CreateLogger();

Unfortunately, I can not figure out where the example is getting 'serviceProvider', which I'm assuming is an IServiceProvider, but would not be available with 'services.AddLogging()' where one would normally set up the logger in a ConfigureServices() scenario.

Another possibility might be to throw in an 'examples' directory for a web application and a non-http application (worker service, console, etc.).

Describe alternatives you've considered I haven't considered alternatives because I wish to set up app insights logging for serilog for ASPNETCore web application.

nblumhardt commented 2 years ago

I think in this case it's most likely within the UseSerilog(...) callback; another snippet in the README shows this:


    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
                .WriteTo.ApplicationInsights(
            services.GetRequiredService<TelemetryConfiguration>(),
            TelemetryConverter.Traces))
            .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

The README needs some work now that 4.0 is out, and TelemetryConfiguration.Active is well and truly in the rear-view mirror. Some help would be appreciated if anyone can bite this off.

lucashby commented 2 years ago

I read that part as being necessary only if you wished to have the bootstrap logger. It appears you are correct and I am getting my application insights logs after switching to UseSerilog(). Thank you for that.