question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"UseSerilog()" Latest version with .net core 2.2

See original GitHub issue

Unfortunately, I can’t upgrade my project to 3.0 yet, which is why I’m still on 2.2. I’ve just installed the latest version of serilog and now “UseSerilog” is invalid. I know that 3.0 changed some of the setup in Program.cs, e.g. changing IWebHostBuilder to IHostBuilder, so I’m wondering whether the latest version is only built for 3.0?

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;
using Serilog.Events;
using System;

namespace myapp
{
    public class Program
    {
        public static int Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();
           
            try
            {
                Log.Information("Starting web host");
                CreateWebHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
       
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseSetting("https_port", "443")
                .UseStartup<Startup>()
                .UseSerilog(); // this line has an error
    }
}

‘IWebHostBuilder’ does not contain a definition for ‘UseSerilog’ and no accessible extension method ‘UseSerilog’ accepting a first argument of type ‘IWebHostBuilder’ could be found (are you missing a using directive or an assembly reference?)

What am I missing? When googling old serilog tutorials they all use “UseSerilog()” for IWebHostBuilder.

Oh and the following packages are installed:

  • Serilog
  • Serilog.Sinks.Async
  • Serilog.Sinks.Console
  • Serilog.Sinks.File

Thanks for some help!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
mslotcommented, Feb 8, 2020

Istead of installing serilog.aspnetcore, you could just do

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                ...
                services.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(Log.Logger, false));

            });

I do this when running console applications, where i don’t need aspnetcore.

9reactions
nblumhardtcommented, Oct 18, 2019

Interesting - the UseSerilog() extension for IWebHostBuilder/.NET Core 2.2 is still supported, could there have been a package installation/upgrade issue? I notice that Serilog.AspNetCore isn’t in the list of packages in your post - just an oversight?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core 2.2 - Serilog | Missing out on events
In my case I have to set Serilog's ILogger from Core library in Startup.cs/Configure() method. Program.cs public static IWebHostBuilder ...
Read more >
Configuring Serilog in ASP.NET Core 2.2 web API
NET Core version 2.2 's web api. Click here to learn how to create a new web API. Why use Serilog? Serilog provides...
Read more >
Setting Up Serilog in a .NET Core 2.2 Web App Deployed ...
Step 1: Install the Following Nuget Packages · Step 2: Make Changes to the appsettings.Development. · Step 3: Create a New or Make...
Read more >
Setting up Serilog in ASP.NET Core - Detailed Beginner ...
This article covers the implementation of Serilog in ASP.NET Core which provides structured logging that is easier to be read by programs.
Read more >
Serilog — simple .NET logging with fully-structured events
NET, Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found