| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics.Tracing; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Text; |
| | 8 | | using Azure.Core.Diagnostics; |
| | 9 | |
|
| | 10 | | namespace Azure.Security.KeyVault.Keys |
| | 11 | | { |
| | 12 | | [EventSource(Name = EventSourceName)] |
| | 13 | | internal sealed class KeysEventSource : EventSource |
| | 14 | | { |
| | 15 | | internal const int AlgorithmNotSupportedEvent = 1; |
| | 16 | | internal const int KeyTypeNotSupportedEvent = 2; |
| | 17 | | internal const int PrivateKeyRequiredEvent = 3; |
| | 18 | | internal const int CryptographicExceptionEvent = 4; |
| | 19 | |
|
| | 20 | | private const string EventSourceName = "Azure-Security-KeyVault-Keys"; |
| | 21 | |
|
| 4 | 22 | | private KeysEventSource() : base(EventSourceName, EventSourceSettings.Default, AzureEventSourceListener.TraitNam |
| | 23 | |
|
| 260 | 24 | | public static KeysEventSource Singleton { get; } = new KeysEventSource(); |
| | 25 | |
|
| | 26 | | [NonEvent] |
| | 27 | | public void AlgorithmNotSupported<T>(string operation, T algorithm) where T : notnull |
| | 28 | | { |
| 44 | 29 | | if (IsEnabled()) |
| | 30 | | { |
| 40 | 31 | | AlgorithmNotSupported(operation, algorithm.ToString()); |
| | 32 | | } |
| 44 | 33 | | } |
| | 34 | |
|
| | 35 | | [Event(AlgorithmNotSupportedEvent, Level = EventLevel.Verbose, Message = "The algorithm {1} is not supported on |
| 40 | 36 | | public void AlgorithmNotSupported(string operation, string algorithm) => WriteEvent(AlgorithmNotSupportedEvent, |
| | 37 | |
|
| | 38 | | [NonEvent] |
| | 39 | | public void KeyTypeNotSupported(string operation, KeyVaultKey key) |
| | 40 | | { |
| 24 | 41 | | if (IsEnabled()) |
| | 42 | | { |
| 24 | 43 | | string keyType = key?.KeyType.ToString() ?? "(null)"; |
| 24 | 44 | | KeyTypeNotSupported(operation, keyType); |
| | 45 | | } |
| 24 | 46 | | } |
| | 47 | |
|
| | 48 | | [Event(KeyTypeNotSupportedEvent, Level = EventLevel.Verbose, Message = "The key type {1} is not supported on thi |
| 24 | 49 | | public void KeyTypeNotSupported(string operation, string keyType) => WriteEvent(KeyTypeNotSupportedEvent, operat |
| | 50 | |
|
| | 51 | | [Event(PrivateKeyRequiredEvent, Level = EventLevel.Verbose, Message = "A private key is required for a {0} opera |
| 58 | 52 | | public void PrivateKeyRequired(string operation) => WriteEvent(PrivateKeyRequiredEvent, operation); |
| | 53 | |
|
| | 54 | | [NonEvent] |
| | 55 | | public void CryptographicException(string operation, Exception ex) |
| | 56 | | { |
| 24 | 57 | | if (IsEnabled()) |
| | 58 | | { |
| 24 | 59 | | string message = FormatException(ex); |
| 24 | 60 | | CryptographicException(operation, message); |
| | 61 | | } |
| 24 | 62 | | } |
| | 63 | |
|
| | 64 | | [Event(CryptographicExceptionEvent, Level = EventLevel.Informational, Message = "A cryptographic exception occur |
| 24 | 65 | | public void CryptographicException(string operation, string message) => WriteEvent(CryptographicExceptionEvent, |
| | 66 | |
|
| | 67 | | private static string FormatException(Exception ex) |
| | 68 | | { |
| 24 | 69 | | StringBuilder sb = new StringBuilder(); |
| 24 | 70 | | bool nest = false; |
| | 71 | |
|
| | 72 | | do |
| | 73 | | { |
| 24 | 74 | | if (nest) |
| | 75 | | { |
| | 76 | | // Format how Exception.ToString() would. |
| 0 | 77 | | sb.AppendLine() |
| 0 | 78 | | .Append(" ---> "); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // Do not include StackTrace, but do include HResult (often useful for CryptographicExceptions or IOExce |
| 24 | 82 | | sb.Append(ex.GetType().FullName) |
| 24 | 83 | | .Append(" (0x") |
| 24 | 84 | | .Append(ex.HResult.ToString("x", CultureInfo.InvariantCulture)) |
| 24 | 85 | | .Append("): ") |
| 24 | 86 | | .Append(ex.Message); |
| | 87 | |
|
| 24 | 88 | | ex = ex.InnerException; |
| 24 | 89 | | nest = true; |
| | 90 | | } |
| 24 | 91 | | while (ex != null); |
| | 92 | |
|
| 24 | 93 | | return sb.ToString(); |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |