Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Core and TelemetryConfiguration.Active is deprecated #141

Closed
ravensorb opened this issue Aug 13, 2020 · 8 comments
Closed

.NET Core and TelemetryConfiguration.Active is deprecated #141

ravensorb opened this issue Aug 13, 2020 · 8 comments

Comments

@ravensorb
Copy link

Given that is TelemetryConfiguration.Active deprecated is there a new recommended approach to integrating Serilog with AppInsights in .netcore in general and azure functions specifically?

@JakeAlmer
Copy link

duplicate of #121

@dglozano
Copy link
Contributor

Have created a PR #160 to update the README with further instructions. You can take a look here.

@raigol
Copy link

raigol commented May 21, 2021

Have created a PR #160 to update the README with further instructions. You can take a look here.

I tried the solution from PR #160 but it gives
"The logger is already frozen." Source "Serilog.Extensions.Hosting"
any suggestions?

@dglozano
Copy link
Contributor

@raigol could you post a code snippet?

@raigol
Copy link

raigol commented May 21, 2021

Program.cs

  public static int Main(string[] args)
    {            
        Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .CreateBootstrapLogger();

        try
        {
            CreateWebHostBuilder(args).Build().Run();
            return 0;
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Orders API terminated unexpectedly");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }



    public static IHostBuilder CreateWebHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog((context, services, loggerConfiguration) =>
            loggerConfiguration
            .WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces))
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            }).ConfigureAppConfiguration((context, builder) =>
            {
                builder.AddJsonFile("azurekeyvault.json", true, true);
                var root = builder.Build();
                builder.AddAzureKeyVault(
                    root["KeyVault--Url"],
                    root["KeyVault--ClientId"],
                    root["KeyVault--ClientSecret"]
                );

                var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development;
                if (isDevelopment)
                {
                    builder.AddUserSecrets<Startup>();
                }
            });

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddLogging(builder => builder.AddApplicationInsights());

@kirill-d-lappo
Copy link

kirill-d-lappo commented Sep 8, 2021

@raigol what version of serilog packages do you use? yesterday I installed the latest ones and I could not use this initialization anymore.

.UseSerilog((context, services, loggerConfiguration) => { /*code here...*/})

Currently UserSerilog can accept only parameter of type Action<WebHostBuilderContext, LoggerConfiguration>
image

@nblumhardt
Copy link
Contributor

The README now shows how to avoid TelemetryConfiguration.Active, so I'll close this, but please see also #156 for some continuing discussions. Thanks!

@dominik-weber
Copy link

@Kira-Lappo i'm late to the party but discovered the same issue a while back. For some reason, the overload for UseSerilog that exposes the service provider is only available for the IHostBuilder, NOT the IWebHostBuilder interface. You'll need to migrate your project to use IHostBuilder instead. The good news is that this migration should be quite painless, at least it was for me. IHostBuilder afaik was introduced with .NET Core 3, here's the migration notes: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-6.0&tabs=visual-studio#hostbuilder-replaces-webhostbuilder

if you're interested, i have posted my code in the discussion of ticket #156

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants