Search code examples
c#.netloggingserilogilogger

Unable to Add File to either ILoggerFactory or SeriLog


I have an API application and I want to write the logs to a file. I am getting errors trying to add file to iLoggingBuilder and to Serilog. The compiler says that the methods, that I have seen used here, do not exist. I am using .Net 7.

Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day) 
.CreateLogger();

'LoggerSinkConfiguration' does not contain a definition for 'File' RollingInterval is also not recognized.

var factory = LoggerFactory.Create(builder => builder.AddConfiguration(conf).AddFile("logs/log.txt")); 

'ILoggingBuilder' does not contain a definition for 'AddFile'


Solution

  • Not sure what Serilog NuGet packages you have installed in your project but for Serilog to write into file you need Serilog.Sinks.File - LoggerConfiguration extension File() is part of that Nuget package.

    Docs