Skip to content

Sink Is Creating New Log Files Within the Day With "_001" at the end #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sunmorgus opened this issue Sep 9, 2016 · 6 comments
Closed

Comments

@sunmorgus
Copy link

I've currently got serilog set up in my Web API project to use the rolling file sink. It configures the logs in the Global.asax Application_Start like this:

        var webPath = HttpContext.Current.Server.MapPath("/logs/");
        var logLevel = ConfigurationManager.AppSettings.Get("LogLevel");

        switch (logLevel.ToLower())
        {
            case "warning":
                Log.Logger = new LoggerConfiguration()
                    .WriteTo.RollingFile(webPath + "BAH.API.Employee-{Date}.txt")
                    .MinimumLevel.Warning()
                    .CreateLogger();

                break;

            case "verbose":
                Log.Logger = new LoggerConfiguration()
                    .WriteTo.RollingFile(webPath + "BAH.API.Employee-{Date}.txt")
                    .MinimumLevel.Verbose()
                    .CreateLogger();

                break;

            case "information":
            default:
                Log.Logger = new LoggerConfiguration()
                    .WriteTo.RollingFile(webPath + "BAH.API.Employee-{Date}.txt")
                    .MinimumLevel.Information()
                    .CreateLogger();

                break;
        }

This works very well; however, there is one issue that I've been seeing... on occasion, it rolls the log file over and appends a "_001", "_002", etc. Is there a way to prevent this from happening? I thought at first that it had to do with the log files getting too big, but it'll sometimes roll them over when there is only a couple lines in there. Do I need to initialize the log file in a different way?

@nblumhardt
Copy link
Member

Hi - thanks for the note. Yes, I think you need to grab version 3.0.0 of the sink and specify shared: true in the configuration, we've just added inter-process shared file support in #15.

Cheers!

@sunmorgus
Copy link
Author

sunmorgus commented Sep 10, 2016

I've tried that I'm afraid, and while it may work, I get errors when trying to later read the log file (I have an endpoint in my API that allows me to get the contents of the log file, since the API is hosted out in Azure and I don't have easy access to it):

The process cannot access the file because it is being used by another process.

I'm using the following code to read the log file:


                    using (FileStream fs = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                        {
                            var log = sr.ReadToEnd();

                            var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
                            response.Content = new StringContent(log, Encoding.UTF8, "text/plain");
                            return response;
                        }
                    }

@nblumhardt
Copy link
Member

Thanks for the follow-up. Sounds like you need to update the version of the Serilog.Sinks.File that's in use - #20 was fixed recently in https://github.com/serilog/serilog-sinks-file/releases/tag/v3.0.1 but the NuGet package dependency version wasn't update here. Fixing that now.

Cheers,
Nick

@chic2013
Copy link

Hi,
Our project is using Serilog2.3.0, Serilog.Sinks.ColoredConsole 2.0.0, Serilog.Sinks.File 3.1.1, Serilog.Sinks.RollingFile 3.2.0. However, i still see that the log/verbose files created for a day have numbers _001, _002, _003 etc attached next to filename-Date.
The idea was to create a central logging web api but when i see these different versions of the files, it kind of negates the very purpose I am creating one. Should not different apps logging into this api be able to write into one single log file for a particular date?
Is there something I might be missing?

Thanks

@nblumhardt
Copy link
Member

Hi @chic2013 - can you please open a fresh ticket with details showing exactly how you're configuring the logger? Thanks!

@chic2013
Copy link

Sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants