I configured Serilog in appsettings.json
to log entries into Logstash via tcp in my asp net core web api
app the following way:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Network" ],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo:0": {
"Name": "TCPSink",
"Args": {
"uri": "tcp://172.26.48.39:5066"
}
},
"Properties": {
"app_id": "my-service-api",
"index": "my-app-"
}
},
...
}
But it logs too many messages. For example, I have a CreateToken action method in Token controller:
[HttpPost]
public ActionResult<string> CreateToken([FromBody] CredentialsModel credentials)
{
var user = _authentication.Authenticate(credentials);
if (user == null)
{
Log.Warning("Unable to authenticate an user: {Login}, {Password}",
credentials.Username, credentials.Password);
return Unauthorized();
}
return BuildToken();
}
I need to log only one message:
Unable to authenticate an user Login Password
But Serilog logs the following:
Request starting HTTP/1.1 POST http://localhost:5000/api/token application/json 57
Route matched with "{action = \"CreateToken\", controller = \"Token\"}". Executing action "Deal.WebApi.Controllers.TokenController.CreateToken (Deal.WebApi)"
Executing action method "Deal.WebApi.Controllers.TokenController.CreateToken (Deal.WebApi)" with arguments (["Deal.BL.Models.Auth.CredentialsModel"]) - Validation state: Valid
Unable to authenticate an user: Login Password
Executed action method "Deal.WebApi.Controllers.TokenController.CreateToken (Deal.WebApi)", returned result "Microsoft.AspNetCore.Mvc.UnauthorizedResult" in 11.0935ms.
Executing HttpStatusCodeResult, setting HTTP status code 401
Executed action "Deal.WebApi.Controllers.TokenController.CreateToken (Deal.WebApi)" in 95.272ms
Request finished in 123.9485ms 401
How can I get rid of unwanted messages?
This is my Program.cs
file:
public class Program
{
public static void Main(string[] args)
{
var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{currentEnv}.json", true)
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
try
{
Log.Information("Start web host");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog()
.UseStartup<Startup>();
}
You can override category-level log messages in your appsettings.json
. For example, the following appsettings.json
sets Microsoft
log messages to only log out for those considered Error
:
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Error"
}
},
...
}
The property of interest here is Override
, which represents an object where the properties are category prefixes and the values are the Serilog log level to use.
Because your own custom log message will not be logged under a Microsoft
category, you will still see that in the output.