0

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:

enter image description here

that means "ex is not NULL here". What is that? How can I deal with this?

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • May be related: https://stackoverflow.com/questions/70955861/the-logging-message-template-should-not-vary-between-calls-ca2254-when-only-pa – phuzi Nov 01 '22 at 16:31
  • Also, take a look at the [documentation for CA2254](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2254) – phuzi Nov 01 '22 at 16:32
  • 4
    That isn't a warning, it's purely informative. CA2254 *is* a warning, but that has nothing to do with `ex` not being `null`. It's a little confusing that both messages appear in the same tooltip. – Jeroen Mostert Nov 01 '22 at 16:32

2 Answers2

1

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.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • I understand the concept and why VS warns me about that, but in practice, how can I deal with that as a best practice? or should I just ignore them? For example, the message in the `await db.Usuario.FindAsync(id)` instruction was removed only if I call that method this way: `await db.Usuario.FindAsync(new object?[] { id, cancellationToken }, cancellationToken: cancellationToken)`. Of course, I did not discovered that by myself. It was VS who suggested me to do that modification. I don't know why, really. – jstuardo Nov 01 '22 at 19:18
  • @jstuardo my understanding is this is purely informational. All it is doing is indicating to you that "_hey, this object is never null on this code path, good job_" (since the only way `ex` is populated is if an exception is caught) Thus, you can ignore it. The documentation seems to indicate that this message can also say something like "object _might_ be null", which could be more useful to a developer (because now you need to account for possible null pointers if you reference the object later on). – Timothy G. Nov 01 '22 at 19:24
1

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);
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • While true, the OP is actually wondering about the other part of that message (the "ex is not NULL here") part. – Timothy G. Nov 01 '22 at 16:51
  • No, they're not, they're wondering about the warning. The warning is not about the nullability. – CodeCaster Nov 01 '22 at 16:52
  • Probably bad wording by the OP, as [this comment](https://stackoverflow.com/questions/74278181/how-to-deal-with-object-is-not-null-here-warning-in-visual-studio-2022-and-ne/74279154#comment131137843_74278181) points out – Timothy G. Nov 01 '22 at 16:53
  • Thanks. I have modified the _logger.LogError code as you suggested, however, those messages appear in other circumstances too, for example, `var usuario = await db.Usuario.FindAsync(id);` where `db` is a database context in EF6. In this case the 3 dots appear below the `db` and hovering the mouse. the message `db is not NULL here` appears. – jstuardo Nov 01 '22 at 18:44
  • @jstuardo see my answer - its the Rosyln analyzer. The warning discussed in this answer is not related, hence why you still see this "object is not null here" message even in other parts of your code. Probably the only way to get rid of this message is disable analysis on files, but if you do that, you will lose some other nice features. – Timothy G. Nov 01 '22 at 19:03
  • @CodeCaster by seeing the LogError method definition, the exception is the first parameter. If I pass the exception as the first parameter, and the message as the second one, the the message appears. why are you passoing the exception as the second parameter? – jstuardo Nov 01 '22 at 19:12
  • @jstuardo then pass the exception first, sorry, typed from memory. – CodeCaster Nov 01 '22 at 21:26
  • @jstuardo that message (not null here) does not belong to the dots. The dots indicate something else. – CodeCaster Nov 02 '22 at 07:22
  • somehow i suppressed this (ex is not NULL here) suggestion, How do i enable it back?? – Sindhoor Dec 18 '22 at 18:42