I am looking for a way to log into both Traces and Exceptions. It seems it is easier to track if I log into both and looks like everyone prefers it. Unless there is a reason not to.
Logger.LogWarning(ex, "Audit failed");
Logger.LogWarning("Audit failed");
The first one logs into exceptions while the second one logs into traces. I would have to add it like this to a lot of places to log it into both. Any other way I missed?
We usually use 2 Logger.LogWarning()
methods if we want to send logs both into traces
and exceptions
.
There is no direct way to do that. You can consider writing an extension method which includes the 2 methods Logger.LogWarning(ex, "Audit failed"); Logger.LogWarning("Audit failed");
. Then you can only call this Extension methods once and send logs into both traces
and exceptions
. The Extension methods
like below:
public static class MyCustomLogger
{
//define an extension method.
public static void Custom_LogWarning(this ILogger logger, string msg)
{
//send this log into traces table
logger.LogWarning(msg);
//send this log into exceptions table
logger.LogWarning(new Exception(), msg);
}
}
Then you can use this extension method like below:
logger.Custom_LogWarning("it is a message from my custom log method...");