I have code in my application which uses system libraries that can throw a CryptographicException
.
In my case these exceptions can occur when
password
for a ssl certificate is malformedpassword
for a ssl certificate is in plain text
but this shouldn't matter since my question is pretty general.
So for the first case the exception that is thrown is
System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.
And for the second case the exception that is thronw is
System.Security.Cryptography.CryptographicException: The input data is not a complete block.
So what I want to do is to log this error into the Windows Event Log and I want to use different event ids based on the error.
So for example, the first error that is thrown shall be logged with event id 1
and the second one with event id 2
.
Also, I don't want to use Exception.Message
to log into the event log since the errors don't show that it's based on the password. I, as a developer, of course know where it's coming from, but when a system administrator checks the logs he won't have any idea what this means. It could mean anything.
However, I looked at the exceptions, and other than the message/stack trace there is no difference between these exceptions.
I saw that there is a HResult
attribute which identifies the excpetion, however when I use e.HResult.ToString("x")
it is 80131501
in both cases (input data not being a complete block and padding being invalid)
So I don't want to use a string comparison to see which event id to throw and which message to use. Is there any other besides the message string to identify which exact message has been thrown?
From other languages, I know that exceptions often have an ErrorCode
attribute which is unique to the exception AND the message.
Is there something similar in C# or how would I identify two different exception messages from the same exception class?