I try to implement different logfiles with Serilog in my ASP Net Core Application, however this seems to be ignored and both loggers write into both files.
Appsettings:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"TCP": "Information",
"REST": "Information"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\path\TCP.log",
"rollingInterval": "Infinite",
"fileSizeLimitBytes": 1073741824, //1GB
"restrictedToMinimumLevel": "Information"
}
},
{
"Name": "File",
"Args": {
"path": "C:\path\REST.log",
"rollingInterval": "Infinite",
"fileSizeLimitBytes": 1073741824, //1GB
"restrictedToMinimumLevel": "Information"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId" ]
},
Program.cs
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.Enrich.WithProcessId()
);
Rest Controller:
public class Controller : ControllerBase
{
private readonly Serilog.ILogger _rest_logger;
public Controller ()
{
_rest_logger = Log.ForContext("SourceContext", "REST");
_rest_logger.Information("Test in Controller");
}
}
TCP Server:
public class TCPServer : IHostedService, IDisposable
{
private readonly Serilog.ILogger _tcp_logger;
public TCPServer()
{
_tcp_logger = Log.ForContext("SourceContext","TCP");
_tcp_logger.Information("TCP Server starting ...");
}
}
Can someone please explain how to fix this and what the issue is
Thank you