Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core - Serilog - IHostBuilder does not contain a definiton of UseSerilog()

trying to follow this tutorial : https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/

I managed to add the Serilog to my app (.NET Core 3.1)

but I cannot modify the CreateHostBuilder function, since it does not recognize UseSerilog() function

I have checked for a solution, but everyone seems to use that code

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

namespace MyApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

            try
            {
                Log.Information("Starting up");
                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Application start-up failed");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
                
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog() // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< unknown function
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

what am I missing here ?

thanks for your help

like image 893
phil123456 Avatar asked Nov 04 '20 10:11

phil123456


People also ask

How is Serilog implemented in .NET core?

Configure Serilog in ASP.NET CoreBy specifying the configuration in the appsettings. json file we will be able to change the configuration or add/remove sinks without code changes. Now change the code in the program. cs to specify Host Builder to use Serilog in ASP.NET Core instead of default logger.

What is Serilog .NET core?

Serilog is a . NET library that provides diagnostic logging to files, the console, and almost everywhere you would like. Serilog can be used in classic . NET Framework applications and for applications running on the latest and greatest .

Why use Serilog?

It's designed to work well with others. Serilog is the reference example of a Structured Logging system - one that doesn't just create messages, it easily captures key attributes and data about the context the message happened in.

What is Hostbuilder in .NET core?

Host Builder is a static class that provides two methods and when we call these methods, they will add some features into ASP.NET Core Applications. The Host Builder Provides two convenient methods for creating instances of IHostBuilder with pre-configured defaults.

Is there a'useserilog'method for iwebhostbuilder?

'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.

How to call useserilog on the host builder?

Calling UseSerilog without any argument should be enough. You need to take on a dependency on Serilog.Extensions.Hosting to be able to call UseSerilog on the Host builder. Thanks for contributing an answer to Stack Overflow!

What is serilog in ASP NET Core?

So Serilog is one of the most popular Libraries for ASP.NET Core Applications. What is Serilog? Console Logging. File Logging. Database Logging. What is Serilog?

How do I use serilog with webapplicationbuilder?

If you're familiar with Serilog configuration in .NET 5, the first thing you'll try to do is call UseSerilog () on WebApplicationBuilder: using Serilog; var builder = WebApplication.CreateBuilder(args) .UseSerilog(...);


1 Answers

Answer provided by Alex Lyalka : packages were missing

dotnet add package Serilog.AspNetCore
like image 78
phil123456 Avatar answered Oct 19 '22 06:10

phil123456