Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Serilog.Extensions.Logging.File

Following the instructions at Nicholas Blumhardt's page and later the Serilog github page, I am having issues getting Serilog to work. Specfically

Error CS1061 'ILoggerFactory' does not contain a definition for 'AddFile' and no extension method 'AddFile' accepting a first argument of type 'ILoggerFactory' could be found (are you missing a using directive or an assembly reference?)

I have downloaded the latest package from from NuGet. My .proj has the following entry <PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />

I've restarted Visual Studio after installing.

Startup.cs (1st Tute)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddFile("Logs/app-{Date}.txt");

Program.cs (2nd Tute)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((hostingContext, builder) =>
        {
            builder.AddFile("Logs/myapp-{Date}.txt");
        })
        .UseStartup<Startup>();

What makes this worse is I did this for a project over a year ago and comparing to that I can't see what I'm doing wrong :(

like image 479
Hecatonchires Avatar asked Jan 28 '23 12:01

Hecatonchires


1 Answers

For one way, follow steps below:

  1. Install package Serilog.Extensions.Logging.File with 1.1.0

    <PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
    
  2. Register AddFile in Startup.cs

        public void Configure(IApplicationBuilder app, IHostingEnvironment env,        
     ILoggerFactory loggerFactory)
    {
        loggerFactory.AddFile("Logs/app-{Date}.txt");
    
like image 132
Edward Avatar answered Feb 06 '23 14:02

Edward