How to read data from multiple database files?

190 views Asked by At

My application uses Entity Framework Core, SQLite and Serilog to log to an SQLite database named Database.db. When Database.db reaches 10 MB Serilog creates a new database and renames the previous Database-someDate.db. I can't read from Database-someDate.db, only from Database.db.

How can I read from these files or force it to only use one file?

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    var connectionString =  ConfigurationManager.ConnectionStrings["SqliteConnectionString"].ConnectionString;
    optionsBuilder.UseSqlite(connectionString);
}
<connectionStrings>
    <clear/>
    <add name ="SqliteConnectionString"
         providerName="System.Data.Sqlite"
         connectionString="Data Source= ./Database.Db"/>
</connectionStrings>

NuGet packages:

enter image description here

Serilog config:

<appSettings>
    <!--serialog-->
    <add key="general:serilog:minimum-level" value="Verbose"/>

    <!--serilog sqlite-->
    <add key="sqlite:serilog:using:SQLite" value="Serilog.Sinks.SQLite"/>
    <add key="sqlite:serilog:write-to:SQLite.sqliteDbPath" value="Database.db"/>
    <add key="sqlite:serilog:write-to:SQLite.tableName" value="Logs"/>
    <add key="sqlite:serilog:write-to:SQLite.storeTimestampInUtc" value="false"/>
    <add key="sqlite:serilog:write-to:SQLite.autoCreateSqlTable" value="true"/>

</appSettings>
2

There are 2 answers

0
Isaev Semyon On BEST ANSWER

found in repository I decided to increase the size of the database, focusing on the size of my application

In appSettings:

    <!--serilog sqlite-->
    <add key="sqlite:serilog:using:SQLite" value="Serilog.Sinks.SQLite"/>
    <add key="sqlite:serilog:write-to:SQLite.sqliteDbPath" value="Database.db"/>
    <add key="sqlite:serilog:write-to:SQLite.tableName" value="Logs"/>
    <add key="sqlite:serilog:write-to:SQLite.storeTimestampInUtc" value="false"/>
    <add key="sqlite:serilog:write-to:SQLite.autoCreateSqlTable" value="true"/>

    <!--parameter responsible for the size of the database file (MB), maximum-10GB -->
    <add key="sqlite:serilog:write-to:SQLite.maxDatabaseSize" value="2048"/>

    <!--parameter responsible for the creating a new file-->
    <add key="sqlite:serilog:write-to:SQLite.rollOver" value="true"/>
1
Panagiotis Kanavos On

Don't use the same SQLite file both for Serilog logging and application data. This is neither an SQLite nor an EF Core problem. The log database is configured and controlled by the Serilog sink.

If you use the Serilog.Sinks.SQLite sink, the log database will be split when it reaches the default maximum size. Old files will get deleted based on a retention threshold too. As the source code shows the default maximum log size is 10 MB.

You don't need to use .NET Framework configuration files either. Serilog works with the .NET Core configuration middleware already. You can use either Serilog.AspNetCore or Serilog.Extensions.Hosting and load settings with .ReadFrom.Configuration(context.Configuration).

In the Serilog service sample, Serilog is configured along with the host :

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices(services => services.AddHostedService<PrintTimeService>())
            .UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(context.Configuration)
                .Enrich.FromLogContext()
                .WriteTo.Console());

The settings are loaded from the same configuration used by the rest of the application. The sample's appsettings.json contains

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Information",
        "System": "Warning"
      }
    }
  }
}