Automated-Fact-Checking-Resources icon indicating copy to clipboard operation
Automated-Fact-Checking-Resources copied to clipboard

Create Database and Loggertable in same Step

Open UrielMhezzek opened this issue 3 years ago • 3 comments

Please describe clearly what the SQL Sink is doing wrong:

The creation of the database and the log table does not happen at the same time. Otherwise i can log, but the table will created on second start of server and after creating database.

Please describe the expected behaviour clearly:

The log table should be created with the database when it is started for the first time and log data.

List the names and versions of all serilog packages used in the project:

<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.6.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.0.1" />

Target framework and operating system:

.NET Core 5.0

Provide a simple reproduction of your application code:

The MigrateDatabase is running after .UseSerilog. In .UseSerilog will be the Table created. I cant find the right way to create database and log in one Step and have a full logger thats works in DataSeeder.

 public static void Main(string[] args)
        {
CreateHostBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    })
                    .UseSerilog((hostingContext, loggerConfiguration) =>
                    {
                        loggerConfiguration = CreateLoggerConfiguration();
                    })
                    .Build()
                    .MigrateDatabase()
                    .Run();
}

private static LoggerConfiguration CreateLoggerConfiguration()
        {
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var isDevelopment = environment == Environments.Development;
            IConfigurationRoot _config = GetConfig(environment);

            var db = _config.GetValue<string>("ConnectionStrings:DefaultConnection");

            return new LoggerConfiguration()
                            .Enrich.FromLogContext()
                            .WriteTo.Console()
                            .WriteTo.Debug()
                            .WriteTo.Seq("http://localhost:5341")
                            .WriteTo.File("Logs\\log.txt", rollingInterval: RollingInterval.Day)
                            .WriteTo.MSSqlServer(
                                connectionString: db,
                                sinkOptions: GetSinkOptions(),
                                columnOptions: GetColumnOptions()
                            );
        }

 private static void CreateLogger()
        {
// Use it before 
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var isDevelopment = environment == Environments.Development;
            IConfigurationRoot _config = GetConfig(environment);

            var db = _config.GetValue<string>("ConnectionStrings:DefaultConnection");

            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.Console()
                .WriteTo.Debug()
                .WriteTo.Seq("http://localhost:5341")
                .WriteTo.File("Logs\\log.txt", rollingInterval: RollingInterval.Day)
                .WriteTo.MSSqlServer(
                    connectionString: db,
                    sinkOptions: GetSinkOptions(),
                    columnOptions: GetColumnOptions()
                )
                .CreateLogger();
        }

UrielMhezzek avatar Oct 16 '21 16:10 UrielMhezzek

Hi @UrielMhezzek thank you for the report. Could you provide a simple sample that I could use to reproduce the problem?

ckadluba avatar Oct 16 '21 23:10 ckadluba

Here it is. I made it so simple i can.

https://github.com/UrielMhezzek/BlazorAppExampleSink

UrielMhezzek avatar Oct 17 '21 11:10 UrielMhezzek

i got the same request...

codehunter13 avatar Feb 26 '22 14:02 codehunter13

We added the feature you requested in PR #466 and created the dev version 6.3.0-dev-00049 on nuget.org.

Please try it and give us some feedback.

ckadluba avatar Feb 07 '23 07:02 ckadluba