This one is driving me crazy and I don't see the reason. Maybe one of you can help me?
I'm trying to activate logging with Serilog within my Razor web. Here is my code in Program.cs
:
var builder = WebApplication.CreateBuilder(args);
// enabling logging
// Filter to exclude access to static content
builder.Logging.ClearProviders();
builder.Host.UseSerilog((ctx, config) =>
{
config
.ReadFrom.Configuration(ctx.Configuration);
});
// doing all the other stuff
var app = builder.Build();
app.UseSerilogRequestLogging();
// doing all the other stuff
Here is my serilog config from appsettings.json
:
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "logs/web_.log",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
],
"Enrich": [ "FromLogContext", "WithThreadId", "WithMachineName" ]
}
Now the crazy point here is, when I am doing this in a custom service class it is working:
public class UserRoleHandler : IUserRoleHandler
{
private ILogger<UserRoleHandler> _logger;
public UserRoleHandler(ILogger<UserRoleHandler> logger)
{
_logger = logger;
}
private void _getData()
{
_logger.LogInformation("start checking group membership.");
}
}
But doing pretty the same in a model class doesn't work.
public class CreateModel : PageModel
{
private ILogger<CreateModel> _logger;
public CreateModel(MyContext context, ILogger<CreateModel> logger)
{
_logger = logger;
_logger.LogInformation("Model created.");
}
public async Task<IActionResult> OnPostAsync()
{
_logger.LogInformation("Start saving my data.");
}
}
While logging from my own service calls is working, nothing is logging when I do this from my Page Model, nether on console nor in the file.
I've tried to change my settings, to check whether they are applied, I can see impact when changing.
I also removed Serilog and the "normal" log writes my output to console.