I am trying to dependency inject Serilog into all my projects in my WebAPI project in .Net Core 3.1.
In my startup.cs, I am setting up the serilog, but possibly incorrectly:
I have:
using Serilog;
And then within:
public void ConfigureServices(IServiceCollection services)
, I have:
services.AddSingleton(Log.Logger);
I also have:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
This, in my controller class, I do the usual:
using Microsoft.Extensions.Logging;
private readonly ILogger _log;
public AuthorisationController(IConfiguration config, ISecurityService securityService, ILogger<AuthorisationController> log)
{
Configuration = config;
_securityService = securityService;
_log = log;
}
Config and SecurityService are OK.
But in my first method, I try to log:
_log.LogInformation("Login attempt");
But nothing appears in my log.
Can anyone spot the mistake?
Not that it matters, but my appsettings has this:
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "===> {Timestamp:HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "Loggly",
"Args": {
"customerToken": "MySuperSecretKey"
}
}
]
}```