'how to specify log based on log level in serilog?

I'm using Serilog as logging framework in my project. My question is as it states in the Title. I want different logs based on my minimum level.

I'm using LoggingLevelSwitch for dynamic log levels while the app is working but I want to customize the log itself based on this switch. For example if my level is debug I want to show the whole object in my log but if it is information level then I just want to show lesser detail about my object or even better change the whole message template. Something similar like this below:

if(logLevelIsAboveDebug)//inf, wrn, err, ftl
 logger.Information(msgTemplateCodes.INF001,myObject.someProperty);
else
 logger.Debug(msgTemplateCodes.DBG001, myObject, secondVariable)

and my template string would be something like this.

INF001 => "Some detail about action. {userKnownVariableGoesHere}"
DBG001 => "Yet another log for same action with more detail. {@myCustomObject} {someVariableDoesntMatter}"

Since not all of my logs are using the same template I guess I can't just configure this at start. I decided to make a method that will take a parameter and check. Something like this:

public static bool isLoglevelEqualOrAboveGiven(LogEventLevel givenLevel)
{                                                                       
    if ((int)givenLevel <= (int)logLevel.MinimumLevel) return true;     
    else return false;                                                  
} 

And now I can just use this method to check which log I should use but is this right?

I don't want to log the whole object if it's in information level I only want the whole object in case something happen and I'm running the app in debug log level in production.

Is this approach correct? should I continue with this? will it cause any problem in my app to check this is many places?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source