|
| 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 Azure.Core.Diagnostics; |
| 7 | + |
| 8 | +namespace Azure.Core.Perf; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Custom event source for Azure Core logging. |
| 12 | +/// </summary> |
| 13 | +internal class CustomEventSource : AzureEventSource |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="CustomEventSource"/> class. |
| 17 | + /// </summary> |
| 18 | + public CustomEventSource() : base("Azure-Core") { } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Logs a request with the old method. |
| 22 | + /// </summary> |
| 23 | + /// <param name="strParam">The string parameter.</param> |
| 24 | + /// <param name="intParam">The integer parameter.</param> |
| 25 | + /// <param name="doubleParam">The double parameter.</param> |
| 26 | + /// <param name="bytesParam">The byte array parameter.</param> |
| 27 | + [Event(1, Level = EventLevel.Informational)] |
| 28 | + public void RequestOld(string strParam, int intParam, double doubleParam, byte[] bytesParam) |
| 29 | + { |
| 30 | + WriteEvent(1, strParam, intParam, doubleParam, bytesParam); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Logs a request with the new method. |
| 35 | + /// </summary> |
| 36 | + /// <param name="strParam">The string parameter.</param> |
| 37 | + /// <param name="intParam">The integer parameter.</param> |
| 38 | + /// <param name="doubleParam">The double parameter.</param> |
| 39 | + /// <param name="bytesParam">The byte array parameter.</param> |
| 40 | + [Event(2, Level = EventLevel.Informational)] |
| 41 | + public void RequestNew(string strParam, int intParam, double doubleParam, byte[] bytesParam) |
| 42 | + { |
| 43 | + WriteEventNew(2, strParam, intParam, doubleParam, bytesParam); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Writes an event with the new method. |
| 48 | + /// </summary> |
| 49 | + /// <param name="eventId">The event ID.</param> |
| 50 | + /// <param name="arg0">The string argument.</param> |
| 51 | + /// <param name="arg1">The integer parameter.</param> |
| 52 | + /// <param name="arg2">The double parameter.</param> |
| 53 | + /// <param name="arg3">The byte array parameter.</param> |
| 54 | + private unsafe void WriteEventNew(int eventId, string arg0, int arg1, double arg2, byte[] arg3) |
| 55 | + { |
| 56 | + if (!IsEnabled()) |
| 57 | + { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + arg0 ??= string.Empty; |
| 62 | + fixed (char* arg0Ptr = arg0) |
| 63 | + { |
| 64 | + EventData* data = stackalloc EventData[5]; |
| 65 | + data[0].DataPointer = (IntPtr)arg0Ptr; |
| 66 | + data[0].Size = (arg0.Length + 1) * 2; |
| 67 | + data[1].DataPointer = (IntPtr)(&arg1); |
| 68 | + data[1].Size = 4; |
| 69 | + data[2].DataPointer = (IntPtr)(&arg2); |
| 70 | + data[2].Size = 8; |
| 71 | + |
| 72 | + var blobSize = arg3.Length; |
| 73 | + fixed (byte* blob = &arg3[0]) |
| 74 | + { |
| 75 | + data[3].DataPointer = (IntPtr)(&blobSize); |
| 76 | + data[3].Size = 4; |
| 77 | + data[4].DataPointer = (IntPtr)blob; |
| 78 | + data[4].Size = blobSize; |
| 79 | + WriteEventCore(eventId, 4, data); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments