I am writing an HTTP triggered Azure Function in C# (.NET v6.0 isolated) and trying to log to multiple outputs (console, file, 2 DB tables) using Serilog. I tried the WriteTo.Conditional command, but when tested it does not write to any of the options. I am using the Log Levels as conditions. The configuration for each of the log outputs used has been tested separately (WriteTo.Console1, WriteTo.File, WriteTo.MSSqlServer) and it works. Below is my configuration inside Program.cs:
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Conditional(
ev =>
{
bool isDebug = ev.Equals(LogEventLevel.Debug);
if (isDebug) { return true; }
return false;
},
wt => wt
.Console()
)
.WriteTo.Conditional(
ev =>
{
bool isDebug = ev.Equals(LogEventLevel.Debug);
if (isDebug) { return true; }
return false;
},
wt => wt
.File("log.txt")
)
.WriteTo.Conditional(
ev => {
bool isInformation = ev.Equals(LogEventLevel.Information);
if (isInformation) { return true; }
return false;
},
wt => wt
.MSSqlServer(
connectionString: dbConnection,
sinkOptions: sinkOptsHead,
columnOptions: columnOptsHead
)
)
.WriteTo.Conditional(
ev => {
bool isWarning = ev.Equals(LogEventLevel.Warning);
if (isWarning) { return true; }
return false;
},
wt => wt
.MSSqlServer(
connectionString: dbConnection,
sinkOptions: sinkOptsDetails,
columnOptions: columnOptsDetails
)
)
.CreateLogger();
I have also enabled the Serilog Self Log option, but nothing is happening this time.
Serilog.Debugging.SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});
The debug commands inside my function are:
logger.LogDebug("C# HTTP trigger function processed a request.");
logger.LogInformation("{GLOBAL_UUID}{USE_CASE}{ORDER_STATUS}{TIMESTAMP}", guid, useCase, orderStatus, DateTime.Now);
logger.LogWarning("{GLOBAL_UUID}{USE_CASE}{REQUEST_UUID}{SERVICE}{END_SYSTEM}{STATE}{PAYLOAD}{TIMESTAMP}{LEVEL_TYPE}",
guid, useCase, Guid.NewGuid(), service, context.GetType, orderStatus, req.Body, DateTime.Now, levelType);
Any ideas?