serilog : logs only after application stop

1.1k views Asked by At

I trust you doing good, I using Serilogs for first time. I use ASP CORE 3.0. I notice, that Serilogs only logs event after I stop the application, It does not log live events.

my appsettings.Development.json

"Serilog": {
"MinimumLevel": {
  "Default": "Information",
  "System": "Warning",
  "Microsoft": "Information",
  "Microsoft.AspNetCore": "Information"
},
"File": {
  "location": "logs/logging_api.log"
}};

Program.cs

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseSerilog((hostingContext, loggerConfiguration) =>
            {
                // About log messges formatting: https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text
                // Implement serilog configurations
                loggerConfiguration
                .ReadFrom.Configuration(hostingContext.Configuration)
                .Enrich.FromLogContext()
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {HttpContext}");
                var logFileLocation = hostingContext.Configuration.GetSection("Serilog:File:Location").Value ??
                    hostingContext.Configuration.GetSection("LOG_FILE_LOCATION").Value;
                if (logFileLocation != null)
                {
                    loggerConfiguration
                        .WriteTo.File(
                            logFileLocation,
                            outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {HttpContext}",
                            rollingInterval: RollingInterval.Day,
                            rollOnFileSizeLimit: true,
                            fileSizeLimitBytes: 50 * 1024 * 1024);
                }
            });

Startup.cs

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseSerilogRequestLogging();

        // Enable default healthcheck
        app.UseHealthChecks("/api/health/check");

        // Enable swagger only for development environments
        // specifying the Swagger JSON endpoint.
        if (this.configuration.GetValue<bool>("EnableSwagger"))
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Logging API V1");
            });
        }

        app.UseRouting();

        // app.UseMiddleware<LoggingApiAuth>();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

Project file

<ProjectReference Include="..\LoggingApi.Data\LoggingApi.Data.csproj" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.0.0-rc4" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="Serilog.Enrichers.AspnetcoreHttpcontext" Version="1.1.0" />

ISSUE :

It logs only after I stop the application

Loaded **'/usr/local/share/dotnet/shared/Microsoft.NETCore.App/3.0.0/System.Net.NameResolution.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

The program '[42520] LoggingApi.Edge.dll' has exited with code 0 (0x0)**.

[18:06:57 INF] Now listening on: http://localhost:5000 [18:06:57 INF] Request starting HTTP/1.1 GET http://localhost:5000/ [18:06:57 INF] Application started. Press Ctrl+C to shut down. [18:06:57 INF] Hosting environment: Development [18:06:57 INF] Content root path: /development/logging-api/LoggingApi.Edge [18:06:57 INF] HTTP GET / responded 404 in 101.3370 ms [18:06:57 INF] Request finished in 795.6172ms 404 [18:06:59 INF] Request starting HTTP/1.1 GET http://localhost:5000/ [18:06:59 INF] HTTP GET / responded 404 in 1.1243 ms [18:06:59 INF] Request finished in 3.2939000000000003ms 404 [18:07:31 INF] Request starting HTTP/1.1 GET http://localhost:5000/api/actionlogs/internal/5db0f148ba2a6056da8463e5 application/json [18:07:31 INF] Executing endpoint 'LoggingApi.Edge.Controllers.ActionLogsController.GetByIdAsync (LoggingApi.Edge)' [18:07:31 INF] Route matched with {action = "GetById", controller = "ActionLogs"}. Executing controller action with signature System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] GetByIdAsync(System.String) on controller LoggingApi.Edge.Controllers.ActionLogsController (LoggingApi.Edge). [18:07:32 INF] Executing ObjectResult, writing value of type 'LoggingApi.Data.Models.ActionLog'. [18:07:32 INF] Executed action LoggingApi.Edge.Controllers.ActionLogsController.GetByIdAsync (LoggingApi.Edge) in 973.6433000000001ms [18:07:32 INF] Executed endpoint 'LoggingApi.Edge.Controllers.ActionLogsController.GetByIdAsync (LoggingApi.Edge)' [18:07:32 INF] HTTP GET /api/actionlogs/internal/5db0f148ba2a6056da8463e5 responded 200 in 1067.5599 ms [18:07:32 INF] Request finished in 1069.1802ms 200 application/json; charset=utf-8

1

There are 1 answers

0
C. Augusto Proiete On

I couldn't reproduce the behavior you describe... You might have something else in your proeject or environment that is causing that. Both Console and File sinks logs are being written to Console and File as expected.

repro

If you can put together an example project on GitHub that shows this behavior, that would help.