| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Diagnostics.Tracing; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | |
|
| | 9 | | namespace Azure.Core.Shared |
| | 10 | | { |
| | 11 | | internal static class EventSourceEventFormatting |
| | 12 | | { |
| | 13 | | public static string Format(EventWrittenEventArgs eventData) |
| | 14 | | { |
| 8 | 15 | | var payloadArray = eventData.Payload.ToArray(); |
| | 16 | |
|
| 8 | 17 | | ProcessPayloadArray(payloadArray); |
| | 18 | |
|
| 8 | 19 | | if (eventData.Message != null) |
| | 20 | | { |
| 6 | 21 | | return string.Format(CultureInfo.InvariantCulture, eventData.Message, payloadArray); |
| | 22 | | } |
| | 23 | |
|
| 2 | 24 | | var stringBuilder = new StringBuilder(); |
| 2 | 25 | | stringBuilder.Append(eventData.EventName); |
| | 26 | |
|
| 12 | 27 | | for (int i = 0; i < eventData.PayloadNames.Count; i++) |
| | 28 | | { |
| 4 | 29 | | stringBuilder.AppendLine(); |
| 4 | 30 | | stringBuilder.Append(eventData.PayloadNames[i]).Append(" = ").Append(payloadArray[i]); |
| | 31 | | } |
| | 32 | |
|
| 2 | 33 | | return stringBuilder.ToString(); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | private static void ProcessPayloadArray(object[] payloadArray) |
| | 37 | | { |
| 52 | 38 | | for (int i = 0; i < payloadArray.Length; i++) |
| | 39 | | { |
| 18 | 40 | | payloadArray[i] = FormatValue(payloadArray[i]); |
| | 41 | | } |
| 8 | 42 | | } |
| | 43 | |
|
| | 44 | | private static object FormatValue(object o) |
| | 45 | | { |
| 18 | 46 | | if (o is byte[] bytes) |
| | 47 | | { |
| 2 | 48 | | var stringBuilder = new StringBuilder(); |
| 16 | 49 | | foreach (byte b in bytes) |
| | 50 | | { |
| 6 | 51 | | stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0:X2}", b); |
| | 52 | | } |
| | 53 | |
|
| 2 | 54 | | return stringBuilder.ToString(); |
| | 55 | | } |
| | 56 | |
|
| 16 | 57 | | return o; |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |