serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
313 stars 100 forks source link

Multiple log files generated at the same time #170

Closed alesshosry closed 4 years ago

alesshosry commented 4 years ago

I am using serilog to log every request and response of my API. I am using .Net Core 2.1, and below is my code implemented in a middleware class:

               ```
               Log.Logger = new LoggerConfiguration()
                    .WriteTo.Async(a => a.File("Logs/myapp.log",
                              rollOnFileSizeLimit: true,                                  
                              fileSizeLimitBytes: Convert.ToInt32(Configuration["MaxLogFileSize"])))
                                //shared:true,
                    .CreateLogger();

                if (context.Response.StatusCode == 200)
                {
                    Log.Information(infoToLog);                        
                }
                else {
                    Log.Error(infoToLog);                        
                }
                Log.CloseAndFlush();

The above code is working perfectly, except when I published the version on the webserver. I realized that SOMETIMES, multiple log files are generated at the same time, without reaching the max file size. When I opened those files, I found some info logged, and the time of logging is the exact time of each request. It seems, for this purpose many files are generated. Is it possible to fix this issue? Did I miss something in the LoggerConfiguration?

Thanks

sungam3r commented 4 years ago

Check your "MaxLogFileSize". It may happen that Convert.ToInt32(Configuration["MaxLogFileSize"]) gives a very small value.

alesshosry commented 4 years ago

No, the value is equal to 262144000 B = 250 MB; Please check the screenshot Logged_Files

alesshosry commented 4 years ago

It worked now. The issue was from my side: The LoggerConfiguration should be defined in the constructor to be defined once, while in my case I have defined it in the method that is called multiple times.