简体   繁体   中英

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

Are you adding Serilog to your LoggerFactory ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM