nju
nju

Reputation: 53

Serilog does not write logs to file

I have solution where are 5 .net-core projects, one - console application and rest are class library. In class Program in main function I have:

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.LiterateConsole()
            .WriteTo.Async(a => a.RollingFile("logs\\myapp-{Date}.txt",
                outputTemplate : "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level} {Type}] {Message}{NewLine}{Exception} {SourceContext}"))
            .CreateLogger();

         Log.Information("Server starting ...");

            //Here I have class which invoke a lot of(6-7) Tasks 
                NewTask task = new NewTask();
                task.Start();
            //And I end my Main function with
         Log.CloseAndFlush();
         Console.ReadLine();

In my all classes when i catch exceptions I've written:

        catch(Exception e)
        {
            Log.ForContext<ClassName>().Error(e, "Somethign went wrong");
        }

In new log I have only "Server starting..." nothing more. What I should to? While I debug I see that my program run lines with catch. Now I have no idea what to do. Maybe someone has similar problem?

EDIT I modeled from here https://github.com/serilog/serilog/wiki/Getting-Started

Upvotes: 3

Views: 9047

Answers (2)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31877

You should only call Log.CloseAndFlush() after all of the tasks have completed. Placing it after the Console.ReadLine() call should demonstrate this.

Upvotes: 2

Dani
Dani

Reputation: 1047

Are you adding Serilog to your LoggerFactory?

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
    loggerFactory.AddSerilog();
}

Upvotes: 2

Related Questions