I am developing a .NET Core 6 application.
In one part of the code I have this:
catch (Exception ex)
{
_logger.LogError(ex.GetMessage());
string error = "Por favor, contáctese con soporte técnico.";
if (ex.InnerException != null)
error = string.Concat("\n\n", ex.InnerException.Message);
return Json($"ERROR: Existió un error al iniciar sesión. {error}");
}
when I pass the mouse over ex
in ex.GetMessage()
, this warning appears:
that means "ex is not NULL here". What is that? How can I deal with this?
CodePudding user response:
CA2254 indicates that log messages shouldn't vary per call, but that parameters should.
So instead of logging ex.Message
, you log something like:
_logger.LogError("Something went wrong while fooing the bar: {message}", ex.Message);
But actually don't, because you don't just want to log the exception message, but the whole thing, depending on your logging configuration; let the logging framework handle exception logging.
So:
_logger.LogError("Something went wrong while fooing the bar", ex);
CodePudding user response:
This is Rosyln's Flowstate or NullableFlowState
. The message is purely informative (it just somewhat looks like an error/warning because of the CA2254 message as well - if it weren't for that, ex
probably wouldn't even have those dots under it).
When the nullable feature is enabled, the compiler will track the flow state of expressions throughout a method, regardless of what the variable was declared as.
You can read some more information about this here.
I think the actual source for this can be found here.