Home > Back-end >  I can't see SeriLog messages after service is run up
I can't see SeriLog messages after service is run up

Time:03-20

I try to learn SeriLog in practice on a Web App. I can't see the messages after it is run up. When I debug, I'm in successfully OnGet() method and trigger the exception. However, both information and error messages aren't shown. I get only --> App is starting up .... message, which is defined in the Program.cs

I expect to see count numbers and the error message as output. What point do I miss? Doesn't it read the appsettings.json? If it is not read, it shouldn't override Microsoft's messages, but it overrides.

Program.cs

using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();


WebApplication app;
try
{
    Log.Logger = new LoggerConfiguration().CreateLogger();
    builder.Host.UseSerilog((
            (ctx, lc) => lc
                .ReadFrom.Configuration(ctx.Configuration))
    );
    
    app = builder.Build();
    app.UseSerilogRequestLogging();
    Log.Information("--> App is starting up ....");
}
catch (Exception e)
{
    Log.Fatal(e, "The app failed to start bruh!!");
    throw;
}
finally
{
    Log.CloseAndFlush();
}



// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();

app.Run();

Index.cshtml.cs

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace SeriLogApp.Pages;

public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;
    }

    public void OnGet()
    {
        _logger.LogInformation("My exercise app web app logging is here <----");

        try
        {
            for (int i = 0; i < 100; i  )
            {
                if (i == 56)
                {
                    throw new Exception("it our on purpose exception");
                }
                else
                {
                    _logger.LogInformation("the value {LoopCountValue}", i);
                }
            }
        }
        catch (Exception e)
        {
            _logger.LogError(e, "--> I caught you man yoooo!! <<***--");
        }
    }
}

appsettings.json

{
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
    "WriteTo": [
      { "Name": "Console" }
    ]
  }
}

The app's csproj content which includes necessary nuget packages.

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
      <PackageReference Include="Serilog.Enrichers.Context" Version="4.2.0" />
      <PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
      <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
      <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
      <PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
    </ItemGroup>

</Project>

CodePudding user response:

Just remove

Log.CloseAndFlush();

line

  • Related