1

I am using rolling file sink.

Following is my code of initialization:

Serilog.Log.Logger = new LoggerConfiguration()
            .WriteTo.RollingFile(@"L:\logs\Api-{Date}.txt", fileSizeLimitBytes: null)
            .CreateLogger();

Here is the line to log:

Log.Information(message);

But after 2 KB, it creates a new file.

I don't know what's going wrong.

Update

This is my logger class:

public class Logger
{
    public Logger()
    {
        Serilog.Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Verbose()
            .WriteTo.RollingFile(@"L:\logs\Api-{Date}.txt", fileSizeLimitBytes: null)
            .CreateLogger();
    }

    public void Log(LogRequestParameters logRequestParameters, LoggingLevels loggingLevel)
    {
        var message = JsonConvert.SerializeObject(logRequestParameters);

        switch (loggingLevel)
        {
            case LoggingLevels.Verbose:
                Serilog.Log.Verbose(message);
                break;

            case LoggingLevels.Debug:
                Serilog.Log.Debug(message);
                break;

            case LoggingLevels.Information:
                Serilog.Log.Information(message);
                break;

            case LoggingLevels.Warning:
                Serilog.Log.Warning(message);
                break;

            case LoggingLevels.Error:
                Serilog.Log.Warning(message);
                break;

            case LoggingLevels.Fatal:
                Serilog.Log.Fatal(message);
                break;
        }
    }
}

This is my caller class:

public class Caller
{   
    logRequestParameters.DateTime = DateTime.Now.ToString();
    logRequestParameters.Level = "Debug";
    logRequestParameters.MachineName = Environment.MachineName;
    logRequestParameters.Type = "Request";
    logRequestParameters.Request = request;

    Logger logger = new Logger();

    logger.Log(logRequestParameters, LoggingLevels.Information);
}
user1780538
  • 900
  • 3
  • 13
  • 22
  • I took your code above and called `Log.Information(new String('x', 1024))` 5 times and it created a single 5K file. Can you provide more details of your application and how you're logging? The code you showed does not split the file after 2k (using Serilog 2.2.1). – PatrickSteele Sep 29 '16 at 16:50

1 Answers1

1

The code is creating a new logger instance for each event. You need to create a single logger and use it for each logging call.

There is some further information on the lifecycle of Serilog loggers in the documentation.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • Thanks! But how can I create a single logger and use it throughout the application. In my current application, I created a logger class. I want to use this on different location. Moreover, I want to create one constructor which will take File Path and Minimum Level as input. So that for different purpose, I can log in different files. – user1780538 Oct 01 '16 at 12:44