0

Is something along these lines possible? Using Visual Studio (and it's kind of language agnostic I guess but I am using C#), precompiling, I would like to somehow inject and replace values with a unique ID (or number is fine!).

E.g.

public void HelloWorld (
{
    {VARIABLE_FOR_UNIQUE_ID_REPLACED_AT_COMPILE_TIME}
    var someVar = "Error Code#";

    console.print(someVar + {VARIABLE_FOR_UNIQUE_ID_REPLACED_AT_COMPILE_TIME})

}

So for every function, I will have {VARIABLE_FOR_UNIQUE_ID_REPLACED_AT_COMPILE_TIME} and it will be a unique ID to that function, in every file, throughout the solution. It doesn't need to be anything complicated like a GUID, ideally it is just an incremental number when added but does not change after adding. This number is used to identify the function for Error Codes - so I can track where the error derived from even though the message is a generic "something went wrong", e.g. "Error Code #445 something went wrong", "Error Code #6778 something went wrong".

PKCS12
  • 407
  • 15
  • 41

2 Answers2

0

I think you should manage that yourself.

Think about it:

If you get error reports in saying "Error Code #445 something went wrong" then first thing you will want to do is search your code base for "445" to find the originating code - but you won't find anything with the scheme you are describing since the actual source of the error will just say {VARIABLE_FOR_UNIQUE_ID_REPLACED_AT_COMPILE_TIME}.

If on the other hand you just manage it yourself and write something like:

int errorCode = 445;

...and always keep that same pattern in your code, then in a debug situation where you need to find 445 you can actually search your code base for 445 and find the place where the error is coming from.

  • Thanks, I did think about this as you suggested. The problem is, in an environment where there are a lot of files and error codes, it's quite difficult and it's possible that finding the last number is quite hard, and a number of Developers could possibly use the same number at the same time. Is it possible that script can be written or some third party tool to retrospectively scan and inject such values? – PKCS12 Oct 09 '19 at 09:45
  • 1
    @Questioner Why not allocate a number to each source file and number the messages with a source file starting at one. Then the error number can be something like `sourceFileNumber * 1000 + errorNumberWithinFile`. That way each file is independent and the many developers only have to make the `errorNumberWithinFile` unique within the file. – AdrianHHH Oct 09 '19 at 11:51
0

In C# you can get the method name from code for example like this:

public void HelloWorld()
{
    ShowError();
}

static void ShowError([System.Runtime.CompilerServices.CallerMemberName] string caller = null)
{
    Console.WriteLine("Error Code for " + caller);
}

It will print: "Error Code for HelloWorld".

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • Thank you. This is useful but not quite what I need. A short value that does not disclose function name would be ideal. That's why a incrementing number or even short GUID would be great. If it can be automatically hardcoded rather than dynamic it is ideal as Devs can find the corresponding line. Actually is it possible to insert line number + filename?! – PKCS12 Oct 09 '19 at 09:47
  • 1
    @Questioner Yes, use CallerLineNumberAttribute and CallerFilePathAttribute: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information – Sergey Vlasov Oct 09 '19 at 16:55