| | 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 | | { |
| 75564 | 15 | | var payloadArray = eventData.Payload.ToArray(); |
| | 16 | |
|
| 75564 | 17 | | ProcessPayloadArray(payloadArray); |
| | 18 | |
|
| 75564 | 19 | | if (eventData.Message != null) |
| | 20 | | { |
| 75564 | 21 | | return string.Format(CultureInfo.InvariantCulture, eventData.Message, payloadArray); |
| | 22 | | } |
| | 23 | |
|
| 0 | 24 | | var stringBuilder = new StringBuilder(); |
| 0 | 25 | | stringBuilder.Append(eventData.EventName); |
| | 26 | |
|
| 0 | 27 | | for (int i = 0; i < eventData.PayloadNames.Count; i++) |
| | 28 | | { |
| 0 | 29 | | stringBuilder.AppendLine(); |
| 0 | 30 | | stringBuilder.Append(eventData.PayloadNames[i]).Append(" = ").Append(payloadArray[i]); |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | return stringBuilder.ToString(); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | private static void ProcessPayloadArray(object[] payloadArray) |
| | 37 | | { |
| 721584 | 38 | | for (int i = 0; i < payloadArray.Length; i++) |
| | 39 | | { |
| 285228 | 40 | | payloadArray[i] = FormatValue(payloadArray[i]); |
| | 41 | | } |
| 75564 | 42 | | } |
| | 43 | |
|
| | 44 | | private static object FormatValue(object o) |
| | 45 | | { |
| 285228 | 46 | | if (o is byte[] bytes) |
| | 47 | | { |
| 32 | 48 | | var stringBuilder = new StringBuilder(); |
| 400 | 49 | | foreach (byte b in bytes) |
| | 50 | | { |
| 168 | 51 | | stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0:X2}", b); |
| | 52 | | } |
| | 53 | |
|
| 32 | 54 | | return stringBuilder.ToString(); |
| | 55 | | } |
| | 56 | |
|
| 285196 | 57 | | return o; |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |